SqlDataAdapter Overview

 

SqlDataAdapter is a bridge between the DataSet and SQL Server, and for retrieving stored data. SqlDataAdapter by using an appropriate mapping Fill Transact-SQL statements to the data source (which can be changed to match the data in the DataSet data source), and Update (it can change the data in the data source to match the data in the DataSet) to provide this bridge. When filling SqlDataAdapter DataSet, it creates the necessary tables and columns (if they do not already exist tables and columns) for the returned data.

 

) We can create the SqlDataAdapter object in the following three ways:

 

Instructions

 

 

1, through the connection string and query 

string strConn = "uid = account; pwd = password; database = database; server = server"; // SQL Server connection string

strSql="SELECT * FROM 表名";

 

SqlDataAdapter da=new SqlDataAdapter(strSql,strConn);

DataSet ds = new DataSet (); // create a DataSet instance

da.Fill (ds, "custom virtual table"); // Use DataAdapter's Fill method (filling), calling SELECT command

 

This approach has a potential drawback. Suppose the application requires multiple SqlDataAdapter object, in this way to create, it will lead to the creation of each SqlDataAdapter when, all at the same time create a new SqlConnection object, method two can solve this problem

 

 

 

2, created by the query and the SqlConnection object

string strConn = "uid = account; pwd = password; database = database; server = server"; // SQL Server connection string

 SqlConnection conn=new SqlConnection(strConn);

 

string strSql="SELECT * FROM 表名";

 SqlDataAdapter da = new SqlDataAdapter(strSql, conn);

DataSet ds = new DataSet (); // create a DataSet instance

da.Fill (ds, "custom virtual table"); // Use DataAdapter's Fill method (filling), calling SELECT command

 

 

 

3, created by SqlCommand Object

 

 

string strConn = "uid = account; pwd = password; database = database; server = server"; // SQL Server connection string 

SqlConnection connSql = new SqlConnection (strConn); // Sql link class instantiation 

connSql.Open (); // open the database 

// no need to use SqlDataAdapter () open from Connection.open, 

// SqlDataAdapter will automatically open and close it. 

string strSql = "SELECT * FROM table"; // to execute SQL statements 

SqlCommand cmd=new SqlCommand(strSql,connsql);

SqlDataAdapter da = new SqlDataAdapter (cmd); // Create a data adapter instance DataAdapter 

DataSet ds = new DataSet (); // create a DataSet instance 

da.Fill (ds, "custom virtual table"); // Use DataAdapter's Fill method (filling), calling SELECT command 

ConnSql.Close (); // close the database

 

 

SqlDataAdapter da = new SqlDataAdapter (strSQL, ConnSql); // Create a DataAdapter data adapter instance DataSet ds = new DataSet (); // create a DataSet instance da.Fill (ds, "custom virtual table"); // use DataAdapter fill method (filling), calling SELECT command ConnSql.Close (); // close the database

note

 

 

If you only need to perform SQL statement or SP, there is no need to use DataAdapter, you can directly use SqlCommand the Execute method of the series. SqlDataadapter role is to achieve a bridge between DB and Dataset: for example, will update the DataSet changes to the database.

 

SqlDataAdapter UpdateCommand of enforcement mechanisms are: when calling SqlDataAdapter.Update (), check all rows in the DataSet, and then perform SqlDataAdapter.UpdateCommand for each modified Row, that is to say if the unmodified data in the DataSet, SqlDataAdapter. UpdateCommand not be executed.

 

Use Points

 

1, the internal SqlDataAdapter SqlDataReader get the data, and the default SqlDataReader not know the name of its database table query corresponding case,

So the following code:

 

 

string strConn = "uid = account; pwd = password; database = database; server = server"; // SQL Server connection string 

strSql="SELECT * FROM 表名"; 

 

SqlDataAdapter da = new SqlDataAdapter(strSql,strConn);

DataSet ds = new DataSet();

da.Fill(ds);

 

 

In the DataSet creates a new DataTable, this will have a new DataTable named CustomerID and CompanyName columns, but the name of the DataTable object is Table, instead we want Customers.

 

 

 

This problem can be solved by adding TableMapping: 

 

string strConn = "uid = account; pwd = password; database = database; server = server"; // SQL Server connection string 

strSql="SELECT * FROM 表名"; 

 

SqlDataAdapter da=new SqlDataAdapter(strSQL,strConn);

da.TableMappings.Add ( "Table", "Customers"); // set the object name

DataSet ds=new DataSet();

da.Fill(ds);

In fact, the most simple way is by overloading using the Fill method by specifying DataTable, like so:

SqlDataAdapter.Fill(DataSet,"MyTableName");

 

This avoids having to use TableMappings collection.

 

 

 

2, when using the Fill mode, you can specify the DataTable, instead of DataSet:

string strConn = "uid = account; pwd = password; database = database; server = server"; // SQL Server connection string 

strSql="SELECT * FROM 表名"; 

 

SqlDataAdapter da = new SqlDataAdapter(strSql, strConn);

DataTable tbl=new DataTable( );

da.Fill (TBL)

3, note that the opening and closing of the connection process

 

 

Before calling the SqlCommand object to execute sql command, you need to ensure open and when SqlConnection object associated with the object, otherwise throws an exception when the method of the SqlCommand executed, but we see in the above code, SqlDataAdapter no such requirement.

 

 

 

If you call the Fill method of the SqlDataAdapter, and SqlConnection its SelectCommand property is off, the SqlDataAdapter will automatically open it, and then submit a query, get the results, and finally close the connection. If before calling the Fill method, SqlConnection is opened, the query is finished, will also be open SqlConnection, SqlDataAdapter That will ensure that the state SqlConnection restored to the original situation.

 

 

 

This can sometimes lead to performance problems, you need to pay attention to, for example, the following code:

 

 

string strConn = "uid = account; pwd = password; database = database; server = server"; // SQL Server connection string 

SqlConnection conn=new SqlConnection(strConn);

SqlDataAdapter daCustomers,daOrders;

 

strSql="SELECT * FROM Customers";

 daCustomers = new SqlDataAdapter(strSql, conn);

 

strSql="SELECT * FROM Orders";

 daOrders=new SqlDataAdapter(strSql, conn);

 

DataSet ds=new DataSet();

 

daCustomers.Fill(ds,"Customers");

 daOrders.Fill(ds,"Orders");

 

 

 

 

This code causes the connection to be opened and closed twice, each time when calling the Fill method. In order to avoid opening and closing the SqlConnection object before calling the Fill method of the SqlDataAdapter object, we can first open SqlConnection object, then close the connection if you want, we can then call the Close method, like this:

 

 

 

cn.Open();

 

daCustomers.Fill(ds,"Customers");

 daOrders.Fill(ds,"Orders");

 

cn.Close();

 

 

 

 

4, call the Fill method multiple times to note the effective data duplication and data update issues

 

 

string strConn = "uid = account; pwd = password; database = database; server = server"; // SQL Server connection string 

 strSql="SELECT * FROM Customers";

 

SqlDataAdapter da=new SqlDataAdapter(strSql, strConn);

 DataSet ds=new DataSet();

 da.Fill(ds,"Customers");

 

//…….

 

da.Fill(ds,"Customers");

 

 

 

 

Our analysis of the above code, by twice calling the Fill method, SqlDataAdapter execute the query twice, and twice to save the query results to the DataSet, the first call creates a new table named Customers in the DataSet. Results of the second call the Fill method of the query is added to the DataSet in the same table, so each customer's information will appear twice in the DataSet! Of course, if the database administrator on the Customers table defines the primary key, the natural SqlDataAdapter when the DataTable, determines duplicate rows, and automatically discarding the old value.

 

 

 

Consider, assume that a particular customer at the time of the first call the Fill method, stored in the database, then the SqlDataAdapter will add it to the new DataTable. If later this client is deleted, then when you call the Fill method, SqlDataAdapter will not find the customer information in the query results a second time, but it will not delete customer information from the DataSet. This leads to the problem of data updates.

 

 

 

Therefore, the recommended practice is, before you call the Fill method, first delete the local cached DataSet data!

————————————————

Disclaimer: This article is CSDN blogger original article "JeanCheng", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.

Original link: https: //blog.csdn.net/gatieme/article/details/20695853

Guess you like

Origin www.cnblogs.com/wfy680/p/12004533.html