Using a DataGrid Control to Access a Database - Pdf 76


Using a DataGrid Control to Access a Database
In this section, you'll learn how to use a DataGrid control to access the rows in a database
table. Follow these steps to create a DataGrid using VS .NET:
1. First, select File ➣ New Project. In the New Project dialog box, select Windows
Application, and enter DataGridWindowsApplication in the Name field.
2. Click OK to continue. Your new project will contain a blank form.
3. Add a DataGrid control to the form by selecting View ➣ Toolbox, selecting a
DataGrid, and dragging it to your form. Figure 6.9
shows a form with a DataGrid.
Make your DataGrid almost as big as your form by dragging the corners of your
DataGrid out to the end of your form.

Figure 6.9: Form with a DataGrid
Next, you'll add a SqlConnection object and a SqlDataAdapter object to your form.

Note You use a SqlConnection object to connect to a SQL Server database, and a
SqlDataAdapter object to move rows between SQL Server and a DataSet object.
You'll learn the details on how to pull rows from the database into a DataSet in
Chapter 10
, and how to push changes made in a DataSet to the database in Chapter
11.
You can drag a table from a SQL Server database onto your form and have the
SqlConnection and SqlDataAdapter objects created in one step. You use Server Explorer
for this. With databases that do not show up in Server Explorer, your choices are limited.
You can use the controls in Data category of the Toolbox to drag each item to your form,
and then set properties for each data object with the Properties window.
Note To open Server Explorer, select View Server Explorer, or press Cntl+Alt+S.
To add a SqlConnection and SqlDataAdapter object to your form, perform the following
steps:
1. Open Server Explorer.

3. Click CommandText, and then click the button with the ellipsis to display the
Query Builder, as shown in Figure 6.13
.

Figure 6.13: The Query Builder
4. You use the Query Builder to define SQL statements. You can type in the SQL
statement, or you can build it up visually. Make sure all the columns are selected
from the Customers table using the Customers box at the top left of the Query
Builder.
5. Click OK to continue.
To check the rows returned by this SELECT statement, perform the following steps:
1. Click the Preview Data link near the bottom of the Properties window. This
displays the Data Adapter Preview dialog box.
2. In the Data Adapter Preview dialog box, click the Fill Dataset button to run the
SELECT statement, as shown in Figure 6.14
.

Figure 6.14: Previewing the rows retrieved by the SELECT statement
3. Click the Close button to close the Data Adapter Preview dialog box.
Next, you need to create a DataSet object. You use a DataSet object to a store local copy
of the information stored in the database. A DataSet object can represent database
structures such as tables, rows, and columns, among others. In this example, you'll use a
DataSet object to store the rows from the Customers table:
1. Click an area of your form outside the DataGrid.
2. Click the Generate Dataset link near the bottom of the Properties window. This
displays the Generate Dataset dialog box.
3. Select the New radio button and make sure the field to the right of this radio
button contains DataSet1, as shown in Figure 6.15
.


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status