< C++ .NET and Using ADO.NET 4 | Main | Web Service 1 >


 

 

C++ .NET And Using ADO.NET 5

 

 

What we have in this page?

  1. Creating the Form...continue

  2. A Very Quick Reference

 

 

12.  Click the Advanced… button to see advanced properties of the connection. Just left as is and click OK button.

 

Advanced properties of the database connection

 

13.  Next click the Test Connection button to test the connection to the data source. If there is any connection error, you need to resolve it. Close the Add Connection dialog box.

 

TEsting a connection to the data source

 

 

14.  You can see the connection string for this connection. If you can’t see it, expand the plus (+) sign next to the Connection string label. Click the Next button.

 

Choosing database connection - generating the OLEDB connection string

 

15.  Now we are going to choose the database objects. Expand the plus sign for the Tables and Views folders. You can see all the Tables and Views objects for Northwind.mdb database.

 

Northwind database objects - Tables and Views

 

16.  The first step is to specify the tables that we want to use. In this case, there’s only one, so expand the Products from the list in the Tables folder. We want to display the product name (ProductName), the unit price (UnitPrice), and the number of units in stock (UnitsInStock), so select these fields from the list. Make sure that you also select the ProductID field as well. We don’t want to display it, but it’s the primary key for the table and the adapter won’t be able to update the table unless you include it. Click the Finish button.

 

Choosing database objects - columns in the table

 

17.  Resize the Form and the DataGridView controls appropriately so that you can see all the columns.

 

DataSet control in DataGridView control that fitted in the Windows Form

 

18.  Build and run you application. The following output should be expected. It seems that by default SELECT ProductID, ProductName, UnitPrice, UnitsInStock FROM Products SQL query has been added.

 

 

 

 

ADO.NET Dataset in DataGridView C++ .NET control

 

19.  Back to the designer and open the DataGridView Tasks. Here, you can edit existing columns properties, add new column. Preview the data and add new SQL query.

 

Opening the DataGridView Tasks

 

20.  We are going to add a button that can sort the UnitPrice ascending. Select the Add Query… menu item. Select New query name: radio button and put MyQuery1 as the query name. Click the Query Builder… button.

 

Creating a new database query using Search Criteria Builder

 

21.  Select the UnitPrice row and set the Sort Type to Ascending and click the Execute Query button. The SQL query script is generated together with the sorted data. Click the OK button.

 

Building and executing a database query using Query Builder

 

22.  In the Search Criteria Builder dialog box you can see the generated query text. Click the OK button to dismiss the Search Criteria Builder dialog box.

 

Building a search criteria of the data using Search Criteria Builder

 

23.  By default a button that contains an event of the new query is added in the ToolStripContainer. Select and right click the MyQuery1 button in the ToolStrip control and select Properties. Set the Text property to Sort Unit Price Asc.

 

Invoking the property page of the button control

 

24.  Next we need to properly re-arrange the ToolStripContainer that contain the button, at the top of the DataGridView control. Open the ToolStrip Tasks and select the Embed in ToolStripContainer.

 

Embedding the ToolStrip in the ToolStripContainer control

 

25.  Next step, clear all the tick boxes except the Top and select Dock Fill in Form.

 

Adjusting the ToolStripContainer at the top of the DataGridView control

 

26.  Finally click the Re-parent Controls in the ToolStripContainer Tasks. The button is nicely fitted at the top of the DataGridView control.

 

Re-parenting the ToolStripContainer control in the Windows form

 

 

The button in the ToolStripContainer at the top of the DataFridView

 

27.  Build and run your application. When you click the Sort Unit Price Asc, the UnitPrice column will be sorted ascending. Keep in mind that based on the DataGridView properties, you can just sorting ascending or descending any of the columns by clicking the column header.

 

 

 

 

C++ .NET - sorting the DataSet UnitPrice ascending in DataGridView control

 

 

C++ .NET - displaying DataSet in Data GridView - a dynamic view

 

A Very Quick Reference

 

To

Do this

Use ADO.NET classes.

If you are using Visual Studio/C++ .NET, you will need to add only a using directive for the appropriate data provider. For example:

 

using namespace System::Data::SqlClient;

 

Connect to a database.

Create a SqlConnection or OleDbConnection object, and configure its ConnectionString property.

Create a command object.

Create a SqlCommand or OleDbCommand object, and configure its CommandText, CommandType, and Connection properties.

Execute a command.

If the command returns a scalar value, call ExecuteScalar. If the command modifies the database, call ExecuteNonQuery. If the command performs a query, call ExecuteReader. Assign the result to a SqlDataReader or OleDbReader object, and use this reader to loop through the result set. For example:

 

OleDbDataReader ^ reader = cmProducts->ExecuteReader();

while (reader->Read())

{

   Console::Write(reader->GetString(0));

}

 

Use data in a disconnected application.

Create a SqlDataAdapter (or OleDbAdapter), and specify commands to access the database. Create a DataSet, and fill the DataSet by using the data adapter. For example:

 

daTitles = gcnew OleDbDataAdapter(

   L"SELECT * FROM Titles", cnNwind);

dsTitles = gcnew DataSet("Titles");

daTitles->Fill(dsTitles);

 

Display a DataSet in a
DataGridView.

Use the DataSource property of DataGridView. For example:

 

dgTitles->DataSource = dsTitles->Tables->default[0]->DefaultView;

 

Table 17

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5

 

 


< C++ .NET and Using ADO.NET 4 | Main | Web Service 1 >