ADO.NET

xiaoxiao2021-03-05  21

ADO.NET

Author: Zheng Zuo 2005-1-1

In 9CBS, some people often ask some ADO.NET issues, especially when developing information management systems, the program is relatively close, in the development of data and interface layers, will encounter a lot of common problems, below Take a look at the data form wizard you can bring to the vs.net.

One. Use wizard

New Windows application, add components to the current project, select Data Form Wizard, here the name is filled in DataForm1.cs. Click Open the Data Form Wizard dialog. Create a new type of data set MyDataSet. Use the Local Data Connection Wizard, here I choose the Northwind library as a data source. Add a Categories table and a Products table with the Categories table and the Products table in the selection table or view. Add a relationship between a table to name CategoryProductrel. Select the display data in one step in the display style is a single record in a single control, which will have a data bind to the text box. You can use data navigation to select the record of the parent table. After the wizard is completed, an OLEDBCONNECTION will generate, several tables generate several OLEDBDataAdapter responsible for obtaining and updating data. In addition, a strong type of data set.

The running interface of the entire program is as follows:

Basic functions include, but wait for you to click a few times, small bugs appear on the program interface, Microsoft may also estimate that no one will have this form to process data, but this is not what we care.

two. Data fill

First look at the data set structure:

A categories table acts as a parent table, the Products table as a sub-table, categoryId is foreign key, established data table relationship.

The related data in the loading button database will be filled into the data set. The process of the loadDataSet () method is as follows:

Use the DataAdatpter.Fill () method to populate data to a temporary data set. If the operation is successful, the merge this temporary data set to the original data set, and the DataGrid is bound by the table relationship.

// Try to fill the data set.

This.FillDataSet (ObjDataSetTemp);

GrdProducts.DataSource = NULL;

// Clear the old record in the data set.

ObjmyDataSet.clear ();

// Merge the record to the main data set.

ObjmyDataSet.merge (ObjDataSetTemp);

GrdProducts.SetDataBinding (ObjmyDataSet, "Categories.categoryProductrel");

In the method of data filling, we noticed

DataSet.enforceconstraints = false;

This step will increase the data fill efficiency.

There is also a detail that explicitly turns off the data connection efficiency when performing more than two DataAdapter data access methods. Because the status of the data connection Connection instance is not changed before and after the data update method of DataAdapter. If the following code is below.

//this.oledbconnection1.open ();

This.OleDbdataAdapter1.Fill (Dataset);

This.OleDbdataAdapter2.Fill (Dataset);

The status of the Connection before execution is turned off, and it is desirable to know that this process performs two open connections to turn off the connection.

In fact, it is enough.

For the strictness of the data, don't forget to add the following code after filling the data.

// Reopen the constraint check.

DataSet.enforceconstraints = true;

If it is direct reading, it doesn't matter.

With data fillers to see the single-value binding and multi-value binding of the data.

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 above code sets the column of the data table to the TextBox's text property.

The multi-value binding of the data is as follows:

GrdProducts.SetDataBinding (ObjmyDataSet, "Categories.categoryProductrel");

It can be seen that the binding data is quite convenient to the relationship.

four. Data browsing

Here is the index of the BindingContext object to get a bindingmanagerbase instance, and bindingmanagerbase.position is what we need, and the data records of a row are displayed via Position.

For example, Next:

This.BindingContext [ObjmyDataSet, "Categories"]. Position = (this.bindingContext [objmyDataSet, "categories"]. Position 1);

Last section:

This.BindingContext [ObjmyDataSet, "Categories"]. Position = (this.objmyDataSet.tables [categories "]. Rows.count - 1);

In addition, the positionchanged () method is called to change the index display between the navigation buttons.

Fives. Data editing

From the addition method we can see the following code:

/ / Clear the current editing content

This.BindingContext [ObjmyDataSet, "Categories"]. EndCurrendedit ();

Often someone in 9CBS why when editing the DataGrid or TextBox, it will only be saved when the edit box will lose the focus. To achieve not changing the focus, save can be implemented by the above code.

The corresponding cancellation is as follows:

This.BindingContext [ObjmyDataSet, "categories"]. CancelcurrendEt ();

The code to delete the data is as follows:

This.BindingContext [ObjmyDataSet, "Categories"]. Removeat (this.bindingContext [ObjmyDataSet, "categories"]. Position);

Seeing the above code found that the original data is not true in the data source deletion, but more than the DATAROW DELETE () method may be submitted to the data source.

six. Data Update

The code to be generated is as follows:

Public void updateDataSet ()

{

// Create a new data set to save the changes to the master data set.

Windowsapplication1.mydataset objDataSetchanges = new windowsapplication1.mydataset ();

/ / Stop current editing.

This.BindingContext [ObjmyDataSet, "Categories"]. EndCurrendedit ();

This.BindingContext [ObjmyDataSet, "Products"]. EndCurrented ();

// Get the changes made to the master data set.

ObjDataSetChanges = ((WindowsApplication1.myDataSet) ());

// Check if any changes have been made.

IF ((ObjDataSetChanges! = null))

{

Try

{

// Need some changes, so try to call Update method

// and pass the data set and any parameters to update the data source.

This.UpdatedataSource (ObjDataSetChanges);

ObjmyDataSet.merge (ObjDataSetChanges);

ObjmyDataSet.acceptchanges ();

}

Catch (System.exception EUPDATE)

{

/ / Add an error handling code here.

Throw EUPDATE;

}

// Add code to check if there is any possible in the returned data set.

// Push into the error in the wrong object error.

}

}

The update process is very classic. By obtaining a modified dataset update subset to the data source completion update action, then the combined subset is obtained to the original data set, by the way, the process of the merge is based on the data table master key. All changes to DataSet have been submitted by calling the DataSet.AcceptChanges () method. Since the last call AcceptChanges. Corresponding to Data.rejectChanges (); all changes to DataSet since the last call DataSet.acceptchange or last call DataSet.AcceptChange.

Seven. supplement

For the update of data, it is necessary to see the program environment, and the method of obtaining a subset can be used, and the multi-data table operation in ADO.NET is designed. Someone asked how to implement such a function: A field data in the database is 0 or 1, and if the program is displayed, it wants to display whether it is, here I think it is best not to do article on the SQL statement. The replacement method is to use the Format events and Parse events of the binding object. Binding.Format event occurs when it is binding the properties of a control to a data value. Binding.parse event occurs when the value of the data binding control changes. There is also a status of each button that can be set by bindingmanagerbase.positionchanged events. You can see the MSDN or "ADO.NET CORE Reference" book. In addition, using try {} catch (system.exception ex) {} or the situation is subject to the situation, it is not possible to make anything through the Exception base class, if you know what type of exception may be thrown, or The more the better, the resulting reverse effect is to write more code. Why do you want to see a book for Applied Microsoft.Net Framework Programming.

转载请注明原文地址:https://www.9cbs.com/read-38305.html

New Post(0)