Creating a DataAdapter Object Using Visual Studio .NET

Creating a DataAdapter Object Using Visual Studio .NET

In this section, you'll learn how to create a DataAdapter using Visual Studio .NET.

Note 

You'll find a completed VS .NET project for this section in the DataAdapter directory. You can open this project in VS .NET by selecting File Open Project and opening the WindowsApplication4.csproj file. You'll need to change the ConnectionString property of the sqlConnection1 object to connect to your database. You can also follow along with the instructions in this section by copying the DataReader directory to another directory and using that project as your starting point.

Open your form by double-clicking Form1.cs in the Solution Explorer window. Next, create a SqlDataAdapter object by dragging a SqlDataAdapter object from the Data tab of the Toolbox to your form. When you drag a SqlDataAdapter object to your form, you start the Data Adapter Configuration Wizard, as shown in Figure 10.8.

click to expand
Figure 10.8: The Data Adapter Configuration Wizard

Click the Next button to continue.

You now select the database connection you want to use, or you can create a new one. Pick your connection to the Northwind database (or create a new connection if you don't have an existing one), as shown in Figure 10.9.

click to expand
Figure 10.9: Choosing your data connection

Click the Next button to continue.

Next, you set your query type to "Use SQL statements." As you can see from Figure 10.10, you set your query type to use SQL statements, create new stored procedures, or use existing stored procedures. The SQL statements or stored procedures are then used in the SelectCommand, InsertCommand, UpdateCommand, and DeleteCommand properties of your SqlDataAdapter object. You'll learn about the latter three properties in Chapter 11; they are used to insert, update, and delete rows.

click to expand
Figure 10.10: Choosing your query type

Make sure you've picked Use SQL statements, and click the Next button to continue.

You now generate the SQL statements for your SqlDataAdapter. You can either enter a SELECT statement directly by typing it or you can press the Query Builder button to build your SELECT statement visually. Enter the SELECT statement, as shown in Figure 10.11, and click the Next button to continue.

click to expand
Figure 10.11: Generating the SQL statements

The SELECT statement you entered is now used to generate the INSERT, UPDATE, and DELETE statements along with the table mappings. Figure 10.12 shows the final dialog box for the Data Adapter Configuration Wizard.

click to expand
Figure 10.12: Final dialog box for the Data Adapter Configuration Wizard

Click the Finish button to complete the Wizard. A SqlDataAdapter object named sqlDataAdapter1 is now added to the tray beneath your form, as shown in Figure 10.13.

click to expand
Figure 10.13: The new SqlDataAdapter object in the tray

Warning 

You need to set the Connection property of the SelectCommand in your sqlDataAdapter1 object to your Connection object before the DataAdapter can access the database. You do this using the Properties window by drilling down from SelectCommand to Connection. You then click the drop-down list, select Existing, and select your SqlConnection object, which should be named sqlConnection1. Also check the ConnectionString property of your SqlConnection object to make sure it is set to connect to your Northwind database. If you don't do this step, you'll get an error stating that your SqlDataAdapter object hasn't been configured properly.

Notice the three links at the bottom of the Properties window for sqlDataAdapter1:

  • Configure Data Adapter This link allows you to re-enter the Wizard to configure your DataAdapter.

  • Generate Dataset This link allows you to generate a DataSet object using the information set for your DataAdapter. You'll use this link in the next section to generate a new DataSet.

  • Preview Data This link allows you to preview the data returned by the SelectCommand of your DataAdapter.

Feel free to examine the code generated by the Wizard in your form for the sqlDataAdapter1 object. When you're ready, select File Save All.

Note 

Don't bother running your project yet because you'll add a DataSet that will be populated using your DataAdapter in the next section.




Mastering C# Database Programming
Mastering the SAP Business Information Warehouse: Leveraging the Business Intelligence Capabilities of SAP NetWeaver
ISBN: 0764596373
EAN: 2147483647
Year: 2003
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net