Ado. Analysis of most table operations in net-read
Source: Internet
Author: User
ado| data ADO. Analysis of most table operations in net-read
Author: Zhengzo??????? 2004-8-5
???????? In the development based. NET Platform database application, we generally use dataset, as the core class of Ado.net it provides us with powerful functions, and the whole looks like a small database in memory, including the DataTable, DataView, DataRow, DataColumn, constraint and DataRelation. I was a little excited when I saw it.
???????? Here are some examples of some of my experiences to illustrate the operations that enable transactions in Ado.net, associated table updates, and multiple command objects during execution. Welcome you to Exchange, or on the blog message.
????????
First, preparatory work
???????? We are familiar with the Northwind database, so take it as an example, I put the Customers (Customer table), Orders (order form), ordering details (Order Detail table) together to establish a typed dataset, The type name is Datasetorders, each table contains only some fields, and the following is in visual Studio. NET to create a screenshot:
?
Figure 1-1
The above set up two relationships expressed as customers-> Orders->order Details. Because the OrderID field of the Orders table is an automatic growth column, the AutoIncrementSeed and AutoIncrementStep values are set to-1, which may be obvious in the actual addition of the order, but it's no problem.
????????
Two Populating data sets
Create a form program to demonstrate the actual operation, the interface is as follows:
Figure 2-1
The entire application is a form whose three DataGrid is used to display data for the related tables, but they interact with each other. The other two radio boxes are used to decide how to update the data, and two buttons just like their name to complete the function.
Here we use a DataAdapter to fill the dataset, and execute the stored procedure as follows:
CREATE PROCEDURE Getcustomerordersinfo
As
SELECT customerid,companyname,contactname from Customers WHERE CustomerID like ' a% '
?
SELECT Orderid,orderdate,customerid from Orders? WHERE CustomerID in
(SELECT CustomerID from Customers WHERE CustomerID like ' a% ')
?
SELECT Orderid,productid,unitprice,quantity,discount from [order Details] WHERE OrderID in
(SELECT OrderID from Orders?) WHERE CustomerID in
(SELECT CustomerID from Customers WHERE CustomerID like ' a% '))
?
Go
?
To reduce the amount of data, only the data CustomerID with ' A ' is taken.
Establish the DataAccess class to manage the interaction between the form and the data tier:
Fork open the topic to mention, Data Access Application Block 2.0 of the Sqlhelper.filldataset this method is more than two tables filled with errors, in fact, the logic inside is wrong, but only two tables when coincidentally, the following is from the inside section of the code:
???????????????????????????????? throw new ArgumentException ("the Tablenames parameter must contain a list of tables, a value was provided as null or empt Y string. "," tablenames ");
??????????????????????????? TableName + = (index + 1). ToString ()//There is an error here
?????????????????????? }
?????????????????? }
?????????????????? DataAdapter.Fill (DataSet);
?????????????????? Command. Parameters.clear ();
????????????? }
????????????? if (mustcloseconnection)
?????????????????? Connection. Close ();
???????? }
?
Here put TableName + = (index + 1). ToString ();
DATAADAPTER.TABLEMAPPINGS.ADD (index>0)? ( Tablename+index. ToString ()): TableName, Tablenames[index]); it will solve the problem.
?
Next look at the code for the form program:
public class Form1:System.Windows.Forms.Form
???? {
???????? Private DataAccess _dataaccess;
???????? Private Datasetorders _ds;
???????? ......
???????? Constructors
???????? Public Form1 ()
???????? {
????????????? InitializeComponent ();
????????????? _dataaccess = new DataAccess ();
????????????? _ds = new Datasetorders ();
????????????? _ds. EnforceConstraints = false; Turn off constraint checking to improve data-filling efficiency
For the dynamic Association of the three tables above, you can also use the SetDataBinding method to complete the dynamic binding of the data instead of specifying the DataSource and Datamemger properties of the datagride separately.
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.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service