ado| data through the Vs.net Data Form Wizard look ado.net
Zhengzo 2005-1-1
On the csdn, people often ask ado.net questions, in particular, the development of information management systems and other closely related to the database program, in the data and interface layer development will encounter a lot of common problems, below we through the vs.net with the data form guide to see what it can help us to refuse what problem.
A Using wizards
Create a new Windows application, add components to the current project, select the Data Form Wizard, and place the name as DataForm1.cs. Click to open the Show Data Form Wizard dialog box. Creates a new typed DataSet myDataSet. Using the Local Data Connection Wizard, here I select the Northwind Library as the data source. Add categories and Products tables in the Select table or view step. Add a relationship between a table named Categoryproductrel. In the Select Display style step, you choose to display the data as a single record in a single control, where data is bound to a text box. You can use data navigation to select a record for the parent table. After the wizard completes, a OleDbConnection is generated, and several tables generate several OleDbDataAdapter responsible for obtaining and updating the data. The other is a strongly typed DataSet.
The operating interface for the entire program is as follows:
Basic features are included, but when you click on a few, the program interface will appear a small bug, Microsoft may also estimate that no one will want the form to process data, but this is not our concern.
Two Data padding
Let's take a look at the dataset structure:
A Categories table as a parent table, a Products table as a child table, CategoryID as a foreign key, the data table relationship established.
The related data from the Load button database is populated to the dataset. The procedure for executing the Loaddataset () method is as follows:
The Dataadatpter.fill () method populates the data to a temporary dataset, and if the operation succeeds, the temporary dataset is merged into the original dataset, and the DataGrid binds the child table through the table relationship.
This step will improve the efficiency of data filling.
Another detail is that it is more efficient to explicitly turn off data connections when performing more than two DataAdapter data access methods. The state of the data connection connection instance will not change until the DataAdapter data Update method is executed and after the method. If the following code.
This.oleDbConnection1.Open ();
This.oleDbDataAdapter1.Fill (DataSet);
This.oleDbDataAdapter2.Fill (DataSet);
The state of connection before execution is closed, and it is conceivable that this process will perform a two-time open connection shutdown connection.
In fact, once is enough.
For the tightness of the data, after filling the data, don't forget to add the following code,
Reopen the constraint check.
DataSet.EnforceConstraints = true;
It doesn't matter if it's read straight.
With the data padding, it looks at the data's single value binding and multivalued binding.
Three Data binding
The single value binding of the data is as follows:
THIS.EDITCATEGORYID.DATABINDINGS.ADD (New System.Windows.Forms.Binding ("Text", This.objmydataset, " Categories.CategoryID "));
THIS.EDITCATEGORYNAME.DATABINDINGS.ADD (New System.Windows.Forms.Binding ("Text", This.objmydataset, " Categories.categoryname "));
The preceding code binds the columns of the data table to the Text property of the TextBox.
The multivalued binding of the data is as follows:
It is quite convenient to bind data through a relationship.
Four Data browsing
The BindingManagerBase instance is obtained from the index of the BindingContext object, and bindingmanagerbase.position is what we need, by position to display a row of data records.
For example, the next article:
This. Bindingcontext[objmydataset, "Categories"]. Position = (this. Bindingcontext[objmydataset, "Categories"]. Position + 1);
Last one:
This. Bindingcontext[objmydataset, "Categories"]. Position = (this.objmydataset.tables["Categories"). ROWS.COUNT-1);
In addition, call the PositionChanged () method to change the index display between navigation buttons.
Five Data editing
From the Add method we can see the following code:
People often ask questions on the csdn why when editing a DataGrid or TextBox, they are saved only when the edit box loses focus. Saving without changing focus can be achieved by the code above.
See the above code found that the original data is not really in the data source deletion, but we develop the time may use more is the DataRow Delete () method, so that can submit data update to the data source.
Six Data Update
The wizard generates the following code:
public void UpdateDataSet ()
{
Create a new dataset to hold the changes made to the primary dataset.
Windowsapplication1.mydataset objdatasetchanges = new Windowsapplication1.mydataset ();
You need to make some changes, so try calling the Update method
and pass data sets and any parameters to update the data source.
This. Updatedatasource (objdatasetchanges);
Objmydataset.merge (objdatasetchanges);
Objmydataset.acceptchanges ();
}
catch (System.Exception eupdate)
{
Add error handling code here.
Throw eupdate;
}
Add code to check if any of the returned datasets might have been
The error that was pushed into the row object error.
}
}
The update process is classic, submitted to the data source to complete the update by acquiring a modified subset of the dataset, then merges the subset into the original dataset, and by the way, the merging process is judged based on the data table primary key. Commit all changes to the dataset since loading the dataset or last called AcceptChanges by calling the Dataset.acceptchanges () method. Corresponds to Data.rejectchanges (), rolling back all changes made to the dataset since the dataset was created or last called Dataset.acceptchanges.
Seven Add
For the update of the data needs depending on the program environment, can not be obtained by a subset of the method of merging, see the ado.net in the majority of the operation of the table-modified a text. There are also questions about how to implement this function: one of the field data in the database is 0 or 1, and you want it to appear on the program display as Yes or no, I think it's best not to make a fuss on the SQL statement, instead of using the binding object's Format event and the parse event. Binding.format event that occurs when a control's properties are bound to a data value. Binding.parse event that occurs when the value of a data-bound control changes. You can also set the status of individual buttons by bindingmanagerbase.positionchanged events. Specific examples are needed to see the MSDN or Ado.net Core Reference book. In addition, the use of Try{}catch (System.Exception ex) {} or depending on the situation, can not blindly all through the Exception base to catch anything, if you know what kind of exception may be thrown, or the more specific the better, The opposite effect is to write more code. Specifically why to do so can see "applied microsoft.net Framework Programming" a book.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.