Xem mẫu

Data Binding Dim strSQL As String = "SELECT * FROM Products" Then, we open a new SQLConnection using the connection string. Dim sqlConn As New SqlClient.SqlConnection(strConnection) sqlConn.Open() The myCommand data command is created by passing it the statement and connection strings. We can then use myCommand to cause the DataReader to execute the SQL statement that we want – in this case, to select all records from the Products table. Dim myCommand As New SqlClient.SqlCommand(strSQL, sqlConn) myCommand.CommandType = CommandType.Text Dim myReader As SqlClient.SqlDataReader = myCommand.ExecuteReader() However, as we learned previously, the DataReaderretrieves the records one at a time in a forward-only and read-only format. The last part of code retrieves each record in the DataReader one at a time and writes the Product Id and Product Name to the Output window using the Console.WriteLine method: Do While myReader.Read Console.WriteLine("Product Id: " & myReader.GetInt32(0) & _ vbTab & "Product Name: " & myReader.GetString(1)) Loop Notice the use of the GetInt32 and GetStringmethods of the DataReader object. These methods can be used because we know the data types of the underlying data fields. By explicitly using the typed accessor methods in this way, we reduce the amount of type conversion that is required when retrieving the data. Last of all, we have the line that closes the DataReader. myReader.Close() It is very important to note that a DataReader is open until you close the connection with such a line of code (or the object gets destroyed and thus the connection is closed at some point during garbage collection). You can modify this simple example for your own purposes, but this should give you an idea of how to a DataReader can be used to rapidly retrieve forward-only, read-only data. The most important idea to take away from this section is that you should use a DataReader whenever possible, and especially when an in-memory copy of data is not required. 45 Chapter 8 Summary In this chapter we`ve covered some crucial data binding concepts as we further developed our Product Management System. We bound our search results to the DataGrid and added the functionality to allow the user to open a specific record in the results list on the Add/View/Edit Products or Suppliers screens. We specifically learned about: Complex data binding to bind controls to more than one element in a DataSet Simple data binding to bind to the property of a control to a single element in a DataSet Creating the Base Data Form for our Product Management System Creating the Add/View/Edit Products and Suppliers Screens that inherit functionality from the Base Data Form Customizing the Add/View/Edit Screens for their specific needs Validating user input using the ErrorProvider Control Filtering and sorting data using DataViews Returning records using the DataReader You should have a pretty good understanding of how to implement complex and simple data binding and should also have working Search Screens. In the next chapter, we will continue with the development of our application and begin implementing functionality to allow the user to update data from the Add/View/Edit Products and Suppliers screens. Exercises 3. What is the difference between complex data binding and simple data binding? Where does property binding fit in? 4. Briefly describe the ErrorProvider control and what it can allow you to accomplish. 5. What is the purpose of a DataView? 6. Briefly describe the DataReader and when it can be used. Can you bind a DataReader to controls on a form such as a DataGrid? When should you use a DataReader versus a DataSet? Answers are available at http://p2p.wrox.com/exercises/. 46 Data Binding 47 Chapter 8 48 Updating the DataSet and Handling Errors In this chapter, we continue developing our Product Management System where we left off in the previous chapter. We will focus on updating data in the DataSet and then on the underlying database based on changes made by the user. More specifically, we will learn about: Updating a DataSetbased on user input Allowing the user to add, edit, and delete data in the DataSet on the Add/Edit/View Products and Suppliers screens Creating a second dataset that contains all changes made by invoking the GetChanges method Checking for errors in the changed dataset by checking the HasErrors property Saving the changes in the DataSet back to the database using Stored Procedures Accepting or rejecting the changes made based on whether the updates were successful Handling any errors that occur Updating the Local Version of the DataSet We now know to update local DataSet that the DataSetis an in-memory copy of data that is not connected to the database from which its contents have come. Thus, if you modify the contents of a DataSet you are not actually updating the data in the underlying data store unless you take additional steps. We will start the latest round of changes to our Product Management System by adding code to enable changes to be made to the local DataSet. Then, we will implement the mechanism that saves all the changes back to the original data source when the user clicks the Save All button. ... - tailieumienphi.vn
nguon tai.lieu . vn