Sử dụng Visual Studio NET Công cụ. Để tăng tốc độ Viết ADO.NET Mã Mã bạn đã viết đến thời điểm này không làm điều đó nhiều. Nó thậm chí không truy cập vào cơ sở dữ liệu. Nhiệm vụ tiếp theo là viết code mà populates lớp với dữ liệu từ cơ sở dữ liệu,
Nội dung trích xuất từ tài liệu:
Use Visual Studio .NET Tools to Speed Up Writing ADO.NET 9.3 Use Visual Studio .NET Tools to Speed Up Writing ADO.NET CodeThe code youve written up to this point doesnt do that much. It doesnt even access thedatabase. The next task is to write code that populates the class with data from thedatabase, and the first step in doing this is setting up database access objects.TechniqueIn Chapter 3, you learned how to fill a dataset to store data in a disconnected fashion. Inthis chapter, you will use a strongly typed dataset-that is, a dataset with data rows thatmatch the name and datatypes of the columns. You will learn how to use the DataAdapterConfiguration Wizard to autogenerate code that initializes Command and DataAdapterobjects for use with a specific table.Steps 1. Right-click on your project and select Add New Item from the Add menu. Choose DataSet and name it dsCustomers.xsd. 2. Visual Studio .NET opens dsCustomers.xsd in Design mode. Expand the Server explorer and drill down to Data Connections, Northwind, Tables. Drag the Customers table onto the Design view. 3. Visual Studio might process for a few seconds, but afterward, youll have a strongly typed dataset. The result? Instead of writing dataset code like this: strCustomerID = CType(ds.Tables(Customers).Rows(0).Item(CustomerID), String) youll have code that looks like this: strCustomerID = ds2.Customers(0).CustomerID A strongly typed dataset is a combination of two documents. One is a .vb file with the same name as the dataset. Visual Studio .NET will not show you the contents of this file (unless you step into it while debugging), and the contents dont appear in the Solution Explorer. The other file is an .xsd file, or a XML Schema Definition (XSD), which defines a data structure. The .vb file reads the XSD to properly instantiate a strongly typed dataset.You should always take a look at any code that is generated automatically by atool because the tool might generate code that doesnt do precisely what you wantit to do. If you have never seen an XSD, this is also a good time to learn somethingnew. Listing 9.18 shows the XSD for the dsCustomers dataset. You can view theXSD you created by opening dsCustomers.xsd from the Solution Explorer andthen clicking the XML button at the bottom of left corner of the screen.Listing 9.18 dsCustomers.xsd: The Customers XSD It is beyond the scope of this chapter and this book to fully explain an XSD. The subject requires an entire book of its own. But its important to point out a few areas of this XSD. The element tags are really nothing more than the properties you have already declared. Each element has a name that corresponds to a column in the Customers table, as well as a type that loosely corresponds to the datatype of the column. The element tag also has a minOccurs attribute. This attribute actually defines whether a value is required for that element. The default for the minOccurs attribute is 1, which means that the element does not allow Null values. Also, take a close look at the lines at the end of the XSD that begin with 10. Now, you have the option of choosing the type of query to use with the data adapter. For the purposes of this example, use SQL statements. The other choices, shown in Figure 9.3, allow you to specify existing stored procedures, or have the wizard create new database stored procedures for you. Figure 9.3. The Choose a Query Type page of the DataAdapter Configuration Wizard.11. The next page of the wizard, shown in Figure 9.4, asks for a SQL statement to retrieve data from the database. Choose Query Builder to create a new query. Figure 9.4. The Generate the SQL Statements page of the DataAdapter Configuration Wizard.12. Using the Query Builder, shown in Figure 9.5, select the Customers table, and return all the columns in the table. Add a WHERE condition to select a single customer row based on the Customer ID as in Listing 9.19. Click Next and then Finish. Listing 9.19 CustomerData.vb: The SELECT Command Text to Use in the DataAdapter Configuration Wizard SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers WHERE CustomerID = ? Figure 9.5. The Query Builder allows you to graphically build a SQL statement.13. Again, Visual Studio picks some undescriptive and bland names for the data adapter and its Select, Update, Insert, and Delete commands. In this example, OleDbAdapter1 will be renamed to odaCustomers, and OleDb[Action]Command1 will be renamed to [Action]Customer.14. Finally, right-click on odaCustomers and select Properties. In the Properties window, expand the TableMappings property, which will open a dialog box like that shown in Figure 9.6. This property tells Visual Studio .NET where to look for datatype and column information. The DataAdapter Configuration Wizard will have referenced the Customers table because it was not aware of the XSD you created earlier in this section. Check on the Use a Dataset to Suggest Table and Column Names check box, and select dsCustomers from the DataSet combo box. Make sure that the DataSet Table combo box references the Customers table of the dataset. Figure 9.6. The Table Mappings collection of the data adapter.Based on the query you entered, Visual Studio .NET will generate Update, Delete, andInsert ...