Creating the Data Connection

Data Connections can be created in several ways. Let's look at how the VS.NET Wizard does this. For this example, we'll create a connection to an Access database. The steps for MS SQL will be slightly different.

1. Open the Server Explorer, and select View | Server Explorer from the menu.

2. Right-click Data Connection, then select Add connection.

3. Select the Provider tab.

4. Select the appropriate provider. For access, select Jet 4.0 OLEDB Provider.

5. Click Next.

6. Select the database name by clicking the Browse... button and navigating to your database.

7. Click Test Connection.You should get a pop-up window that says Connection succeeded.

9. Click OK.You now have a data connection.

While in design mode, you can drag and drop this connection onto your .asmx page.This will add the following to our code-behind page as the first line in the service public class:

private System.Data.OleDb.OleDbConnection oleDbConnectionl;

Connection string information will also be added to the InitializeComponent() method. Alternatively, we can still create a connection string by creating a .udl file on the desktop, double-clicking it and following the dialogs. With this method, we will have to insert the code ourselves, as follows:

1. In C#, add Using System.Data.OleDb to the top "using" section.

2. Then add the following inside the service class:

private OleDbConnection myConnection = new OleDbConnection();

3. Add the following to a method (Page_onload, or a method of your own creation):

myConnection.ConnectionString = [the string obtained from the udl file]

We will take a closer look at adding a connection when we create the "sellerAdmin" service in the next section.

0 0

Post a comment