Working with ADO.NET - Connecting to the database
(Page 2 of 4 )
In order to establish a connection to the database you have to follow a three step procedure as outlined below.
The first step is to add the OleDbDataAdapter control to the form. Double click the control from the Data section of the Toolbox (see figure 3).

The Data Adapter Configuration Wizard opens up (see figure 4).

Click Next to continue and select the New Connection button as shown in the figure below.

Select the Provider tab from the DataLink Properties dialog and click the Microsoft Jet 4.0 OLE DB Provider option (see figure 6).

Click the Next button to continue and select your MS Access database, which will be used for this application. You can click the Test Connection button to verify the connection, which will show any errors which will possibly occur while establishing a connection to a database (see figure 7).

Click the Next button on the adapter wizard until you see a dialog called Generate the SQL Statements. You can use the Query Builder to generate the required SQL Statement. Add the table name from the Add Table dialog and click the * mark on the builder. The system will generate a SQL statement similar to one shown below:
SELECT <Your Table Name>.*
FROM <Your Table Name>
The above statement will be sufficient for our sample application. You can now click the Next button on the wizard and the whole process is over as soon as you click the Finish button. The wizard will display any errors which may occur during the generation of the SQL statement. I have not selected a column as primary key while creating the database. Hence the wizard will show a warning. This is not an error (see figure 8).

The second step is to add the OleDBConnection control to the form. You need not perform any additional work for this control because this will be used for connecting to the database.
The final step is to generate a dataset. Select the Generate DataSet menu option from the Data menu. The Generate DataSet dialog opens. Choose the option New, give a meaningful name, and close the dialog by clicking the OK button.
You will notice that all three Data controls will appear on the bottom tray of the form, as they are not visible during run time. The above procedure is not yet complete, because you have to fill the data adapter with the dataset. This can be done using one line of code as shown below:
OleDbDataAdapter1.Fill(DsCustomers1)
You need not provide any further code because everything will be automatically done in the background by Visual Studio .NET 2003.
Next: Inserting Records >>
More .NET Articles
More By Anand Narayanaswamy