1. Trang chủ
  2. » Công Nghệ Thông Tin

database aplication by .net

49 107 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 49
Dung lượng 390,14 KB

Nội dung

murach’s TRAINING & REFERENCE database programming with ADO.NET Anne Prince Doug Lowe VB.NET Mike Murach & Associates 2560 West Shaw Lane, Suite 101 Fresno, CA 93711-2765 (559) 440-9071 • (800) 221-5528 murachbooks@murach.com • www.murach.com Copyright © 2003 Mike Murach & Associates. All rights reserved. 3 How to develop a simple database application Now that you know the concepts and terms that you need for developing database applications with ADO.NET, this chapter shows you how to develop simple database applications of your own. First, you’ll learn how to use the Data Adapter Configuration Wizard to create the data adapter, command, and connection objects. Then, you’ll learn how to create and work with a dataset and how to use a data grid control to work with the data in the dataset. How to use the Data Adapter Configuration Wizard 46 How to start the wizard 46 How to define the connection 48 How to define the SQL statements 50 How to use the Query Builder 52 How to control the Insert, Update, and Delete statements that are generated 54 How to complete the configuration 56 The objects created by the Configuration Wizard 58 How to work with a dataset 60 How to generate a dataset 60 The dataset schema file, class, and object 62 How to load and unload a dataset 64 How to update the database with changes made to a dataset 66 How to use a data grid control with a dataset 68 A Vendor Maintenance form that uses a bound data grid control 68 How to bind a data grid control to a dataset 70 The code for the Vendor Maintenance form 72 How to work with the data in a data grid control 74 How to customize a data grid control 76 Basic properties for customizing a data grid 76 How to use table styles to customize a data grid 78 How to use column styles to customize a data grid 80 Other skills for working with ADO.NET objects 82 How to preview the data in a data adapter 82 How to view the dataset schema 84 How to review the properties for generated ADO.NET objects 86 How to interpret the generated SQL statements 88 Perspective 90 46 Section 1 An introduction to database programming and ADO.NET How to use the Data Adapter Configuration Wizard The easiest way to create the components you need to work with a database is to use the Data Adapter Configuration Wizard. The topics that follow lead you step-by-step through the process of using this wizard. When you’re done, you’ll have the data adapter, command, and connection objects you need for develop- ing a database application. How to start the wizard To start the Data Adapter Configuration Wizard, display the Toolbox, click on the Data tab to display the data components, and then double-click on the data adapter component you want to use. When you do that, the first dialog box of the wizard is displayed as shown in figure 3-1. This dialog box describes the function of the wizard. This also adds a data adapter to the Component Designer tray at the bottom of the designer window. Then, as you proceed through the wizard’s dialog boxes, the data adapter is configured depending on the information you provide. To proceed with the configuration, you can click on the Next button. If you want to define the data adapter and the other data components without using the wizard, you can click on the Cancel button from this dialog box. Then, you can use the Properties window to set the properties for the data adapter, and you can use the Data tab of the Toolbox to create the other ADO.NET components. Because the wizard quickly and easily creates all the components you need, however, I recommend that you use it whenever possible. Chapter 3 How to develop a simple database application 47 The dialog box that’s displayed when you start the Data Adapter Configuration Wizard Description • The Data Adapter Configuration Wizard helps you create the data adapter, connection, and command objects for working with a database. • To start the wizard, simply double-click on the OleDbDataAdapter or SqlDataAdapter component in the Data tab of the Toolbox. The data adapter is then added to the Compo- nent Designer tray at the bottom of the designer window, and the first dialog box of the wizard is displayed. • The first dialog box displays a welcome message and describes the function of the wizard. To continue with the wizard, click on the Next button. • If you click on the Cancel button from the wizard’s Welcome dialog box, the wizard is canceled. Then, you can set the properties of the data adapter from the Properties win- dow, and you can create the other ADO.NET objects using the components in the Data tab of the Toolbox. Figure 3-1 How to start the wizard 48 Section 1 An introduction to database programming and ADO.NET How to define the connection The next dialog box helps you define the connection object as illustrated in figure 3-2. From this dialog box, you can select an existing connection (one you’ve used previously), or you can click on the New Connection button to display the Data Link Properties dialog box shown here. This dialog box helps you identify the database that you want to access and provides the information you need to access it. When the Data Link Properties dialog box is first displayed, the Connection tab is visible. In this tab, you select the name of the server that contains the database you want to access; enter the information that’s required to log on to the server; and select the name of the database you want to access. How you do that, though, varies depending on whether you’re using MSDE on your own PC or whether you’re using a database on your company’s or school’s computer. If you’re using MSDE on your own PC and you’ve installed MSDE and attached the Payables database to it as described in Appendix A, the server name should be the name of your computer followed by \VSdotNet. On my computer, for example, the server name is ANNE\VSdotNet. Next, for the logon information, you should click on the Use Windows NT Integrated security button. Then, MSDE will use the login name and password that you use for your computer as the name and password for the database too. As a result, you won’t need to provide a separate user name and password in this dialog box. Last, you select the name of the database that you want to connect to. When you’re done, you can click on the Test Connection button to be sure that the connection works. In contrast, if you’re using a database that’s on your company’s or school’s server, you need to get the connection information from the network administra- tor, the database administrator, or your instructor. That will include the server name, logon information, and database name. Once you establish a connection to a database, you can use that connection for all of the other applications that use that database. If you use the SQL Server data provider to access a SQL Server database, the data provider communicates directly with the database. If you use the OLE DB data provider, though, that data provider communicates with the OLE DB provider for the database you’re accessing. In that case, you need to use the Provider tab of the Data Link Properties dialog box to select the provider you want to use. This tab lists the available providers, and the default provider is the one for SQL Server. Chapter 3 How to develop a simple database application 49 The dialog boxes for defining a connection Figure 3-2 How to define the connection Description • The Choose Your Data Connection dialog box asks you to identify the data connection you want to use. If you’ve already defined a data connection, you can select it from the drop-down list. Otherwise, you can click on the New Connection button to create a connection. • When you click the New Connection button, the Connection tab of the Data Link Properties dialog box is displayed. You use this dialog box to provide the information that’s needed to connect to the database. • If you’re creating an OleDbDataAdapter, the default is to use the OLE DB provider for SQL Server. If that’s not what you want, you can select a different OLE DB provider from the Provider tab. The information that’s required on the Connection tab will vary depending on the provider you choose. • To be sure that the connection is configured properly, you can click on the Test Connec- tion button. Then, click on the OK button to return to the wizard. 50 Section 1 An introduction to database programming and ADO.NET How to define the SQL statements The next two dialog boxes let you define the SQL statements that your program will use to work with the database. The three options in the first dialog box let you use SQL statements, create new stored procedures, and use existing stored procedures. For now, you’ll learn how to use SQL statements. You’ll learn more about working with stored procedures in chapter 8. If you select the Use SQL statements option and click on the Next button, the wizard displays the second dialog box in this figure. This dialog box lets you enter the Select statement you want to use to retrieve data. Alternatively, you can click on the Query Builder button to use the Query Builder to build the Select statement as described in the next figure. Because that’s the easiest way to create the Select statements that you need, that’s what you’ll usually do. The Select statement in this figure selects several columns from a table named Vendors, and it sorts the rows by the VendorName column. You saw this table in chapter 1. Notice that the primary key column, VendorID, isn’t included in this query. Because the primary key is needed to perform update and delete operations, however, the wizard will add it for you. Before I go on, you should realize that Select queries can have a significant effect on the performance of a client/server application. The more columns and rows that are returned by a query, the more traffic the network has to bear. When you design a query, then, you should try to keep the number of columns and rows to the minimum required by the form. Chapter 3 How to develop a simple database application 51 The dialog boxes for defining the SQL statements Description • You use the Choose a Query Type dialog box to specify whether the database will be accessed using SQL statements, new stored procedures, or existing stored procedures. • If you choose to use SQL statements, the Generate the SQL statements dialog box is displayed when you click on the Next button. This dialog box lets you enter the Select statement that will be used to retrieve the data. You can also click on the Query Builder button from this dialog box to build the Select statement interactively (see figure 3-4). • If you don’t include a table’s primary key in the Select statement, the wizard adds it for you. Then, it generates Insert, Update, and Delete statements based on the Select state- ment. If that’s not what you want, you can change the advanced options as described in figure 3-5. • If you choose to create new stored procedures, a dialog box is displayed that lets you enter a Select statement. Then, after you enter the names you want to use for the stored procedures, the wizard generates those stored procedures with the appropriate Select, Insert, Update, and Delete statements. • If you choose to use existing stored procedures, the wizard displays a dialog box that lets you select the stored procedures to use for select, insert, update, and delete operations. Figure 3-3 How to define the SQL statements 52 Section 1 An introduction to database programming and ADO.NET How to use the Query Builder Figure 3-4 shows the Query Builder window you can use to build a Select statement. You can use this graphical interface to create a Select statement without even knowing the proper syntax for it. Then, when you get the query the way you want it, you can click on the OK button to return to the wizard and the Select statement will be entered for you. This is usually easier and more accu- rate than entering the code for the statement directly into the wizard dialog box. When the Query Builder window first opens up, the Add Table dialog box is displayed. This dialog box lists all of the tables in the database you selected. Then, you can use it to add one or more tables to the diagram pane of the Query Builder window so you can use them in your query. In this figure, for example, you can see that the Vendors table has been added to the diagram pane. In the grid pane, you can see the columns that are going to be included in the query. To add columns to this pane, you just check the boxes before the column names that are shown in the diagram pane. You can also enter an expression in the Column column to create a calculated column, and you can enter a name in the Alias column to give the calculated column a name. Once the columns have been added to the grid pane, you can use the Sort Type column to identify any columns that should be used to sort the returned rows and the Sort Order column to give the order of precedence for the sort if more than one column is identified. Here, for example, the rows will be sorted in ascending sequence by the VendorName column. Similarly, you can use the Criteria column to establish the criteria to be used to select the rows that will be retrieved by the query. For example, to retrieve only the rows for California vendors, you can specify “CA” in the Criteria column for the VendorState column. Since no criteria are specified in this query, all of the rows will be retrieved. As you create the query, the SQL pane shows the current version of the resulting Select statement. You can also run this query at any time to display the selected rows in the results pane. That way, you can be sure that the query works the way you want it to. Chapter 3 How to develop a simple database application 53 The Query Builder window Description • When you first start the Query Builder, the Add Table dialog box is displayed. You can use this dialog box to select the tables you want to include in the query. Then, those tables are displayed in the diagram pane. • To include a column from a table in the query, click on the box to its left. Then, that column is added to the grid pane. You can also select all the columns at once by check- ing the * (All Columns) item. • To create a calculated column, enter an expression in the Column column and then enter the name you want to use for the column in the Alias column. • To sort the returned rows by one or more columns, select the Ascending or Descending option from the Sort Type column for those columns in the sequence you want them sorted. You can also use the Sort Order column to set the sort sequence directly. • To specify selection criteria (like a specific value that the column must contain to be selected), enter the criteria in the Criteria column. • To use a column for sorting or for specifying criteria without including it in the query results, remove the check mark from the Output column. • As you select columns and specify sort and selection criteria, the Query Builder builds the Select statement and displays it in the SQL pane. • You can also use the Query Builder shortcut menu to work with a query. To display the results of a query, for example, select the Run command from this menu. The results are displayed in the results pane. Figure 3-4 How to use the Query Builder Diagram pane Grid pane SQL pane Results pane [...]... information that’s generated for these columns by the database is available from the dataset Similarly, the wizard adds a Select statement after the Update statement that’s used to modify a row so any column values that are calculated by the database will be available from the dataset Of course, if the values that are generated by the database aren’t used by your application, it isn’t necessary to refresh... concurrency exceptions that are thrown by an application As you will see in the next chapter, then, database programming can get complicated in a hurry Chapter 3 How to develop a simple database application The Visual Basic code for the Vendor Maintenance form Public Class Form1 Inherits System.Windows.Forms.Form Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles... Then, if the table being refreshed is defined with a primary key, the rows in the existing table are updated by the rows in the database table That, however, can be a time-consuming process if the table contains a large number of rows In addition, rows that have been deleted from the database table by other users will still be included in the table in the dataset That’s why we recommend that you clear... be modifying the same database data at the same time, you may want to refresh the entire data table periodically so it reflects the changes made by other users Chapter 3 How to develop a simple database application How to load a data table The basic syntax of the Fill method for a data adapter dataAdapter.Fill(dataSet[.dataTable]) A statement that loads the data table defined by a data adapter into... operation for a table with a primary key Figure 3-10 How to load and unload a dataset 65 66 Section 1 An introduction to database programming and ADO.NET How to update the database with changes made to a dataset Figure 3-11 shows how to use the Update method of a data adapter to update a database with the changes made to a dataset Notice in the syntax of this method that, just like the Fill method, you... identified by the data adapter is used to update the database When you execute the Update method, Visual Basic checks the RowState property of each row in the table to determine if it’s a new row, a modified row, or a row that should be deleted If it’s a new row, the Insert statement identified by the InsertCommand property of the data adapter is used to add the row to the table in the database Similarly,... executed, a connection to the database is opened automatically The connection is closed when the update is complete • The Update method is implemented as a function that returns an integer value with the number of rows that were updated Figure 3-11 How to update the database with changes made to a dataset 67 68 Section 1 An introduction to database programming and ADO.NET How to use a data grid control... required to display the data or to implement the add, modify, and delete operations • When this form is first loaded, it retrieves data from the database and stores it in the data table used by the data grid • When the user clicks on the Update Database button, the database is updated with changes made to the dataset The user can click on this button after one row or a group of rows have been added, changed,... to work with the data adapter Visual Basic bug • Occasionally, the command objects created by the Configuration Wizard appear in the Component Designer tray, but most of the time they don’t Figure 3-7 The objects created by the Configuration Wizard 59 60 Section 1 An introduction to database programming and ADO.NET How to work with a dataset Each data adapter defines a single result table that can be... Handles MyBase.Load SqlDataAdapter1.Fill(DsVendors1) End Sub Private Sub btnUpdate_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnUpdate.Click If DsVendors1.HasChanges Then SqlDataAdapter1.Update(DsVendors1) End If End Sub Private Sub btnExit_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub End Class Description . a simple database application Now that you know the concepts and terms that you need for developing database applications with ADO .NET, this chapter shows you how to develop simple database applications. column values that are calculated by the database will be available from the dataset. Of course, if the values that are generated by the database aren’t used by your application, it isn’t necessary. Section 1 An introduction to database programming and ADO .NET How to use the Data Adapter Configuration Wizard The easiest way to create the components you need to work with a database is to use the

Ngày đăng: 18/04/2014, 10:16