Summary
for a variety of reasons, has not updated the blog for a long time! Period, a bit confused, a bit lost. Thankfully, there are a lot of enthusiastic readers in the garden who are expecting me to update my blog. Think of their own articles, can help the garden friends, I again picked up a long-lost style of writing, the way, simple and not affectation. in the previous article, "You Must Know ADO (eight) in-depth understanding of DataAdapter", I mainly introduce the working principle and basic functions of DataAdapter. In this article, I'll explain how to use the DataAdapter object in the same way as an instance.
Directory
- How do I construct a DataAdapter object?
- Populating data into a dataset
- Summarize
1. How to construct a DataAdapter object?
Before talking about how to get data with DataAdapter, let's talk about a problem: how to construct a Dataapdater object? We know that DataAdapter is a class, so how do you create an object of a class (again, the comparison basis for this series of tutorials, mainly for beginners)? Oh, of course, with the constructor! Therefore, it is necessary to understand the DataAdapter constructor. Through the previous knowledge, we have been very clear about the different data sources, ADO. NET provides different data Provider. As an example of a SQL Server database, it has DataAdapter as the SqlDataAdapter class, which includes the following constructors:
Public SqlDataAdapter (); Public SqlDataAdapter (SqlCommand SelectCommand); Public SqlDataAdapter (string selectcommandtext, SqlConnection selectconnection); Public SqlDataAdapter (stringstring selectconnectionstring);
For the understanding of constructors, we mainly look at what parameters he contains. From the above we can see that the SqlDataAdapter class contains 4 explicit constructors. Therefore, we must instantiate the SqlDataAdapter class (Create SqlDataAdapter object) with one of the 4 constructors above.
Tip: If there is no declaration constructor in the class, the default constructor (no parameters, no method body) is used when the object is created.
if a constructor (one or more) is declared in a class, you can only instantiate the class with the declared constructor (the system will not create a default constructor).
1.1 SqlDataAdapter ()
The first constructor is relatively simple and does not contain parameters. We can easily get a SqlDataAdapter object:
New SqlDataAdapter ();
Ok,so easy!
1.2 SqlDataAdapter (SqlCommand SelectCommand)
The second constructor is slightly more complex, and it contains a parameter of type SqlCommand. We can create a Sqldataadatpter object like this:
Newnew SqlDataAdapter (CMD);
1.3 SqlDataAdapter (String selectcommandtext, SqlConnection selectconnection)
The third constructor, which contains 2 parameters. The first argument is a string type, which accepts a query instruction. The 2nd parameter is a SqlConnection type, which represents a connection object. OK, let's just write an example!
string " SELECT * from Tb_selcustomer " newnew SqlDataAdapter (SELCMDSTR, Conn);
1.4 SqlDataAdapter (String selectcommandtext, String selectconnectionstring)
The last constructor, which also contains 2 parameters. 2 parameters are string types: the first parameter, which accepts a query instruction, and the second argument, which represents a connection string. Also, let's write an example.
string " SELECT * from Tb_selcustomer " ; string @" Data source=.\sqlexpress; Initial Catalog=db_mydemo; Integrated security=sspi"; // constructing the connection string New SqlDataAdapter (Selcmdstr, connstr);
2. Populating data into a dataset
Above I have described in detail how to construct a DataAdapter object. The reason is very detailed, because only constructs good one DataAdapter object, we can play out its function. One of its most important functions is to populate the dataset with data. The SqlDataAdapter class contains several fill () methods (method overloads)in addition to the 4 constructors. in General , The more commonly used is the int fill (DataTable dt), int fill (DataSet ds), which 2 methods accept each The DataTable parameter and the DataSet parameter. with the Fill () method, we can easily populate the data in the external data source into a dataset. Needless to say, write an example to deepen your understanding.
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Data;6 usingSystem.Data.SqlClient;7 8 namespaceDataAdapter29 {Ten class Program One { A Static voidMain (string[] args) - { - stringConnStr =@"Data source=.\sqlexpress; Initial Catalog=db_mydemo; Integrated Security=sspi";//Connection String the stringSelcmdstr ="SELECT * from Tb_selcustomer";//Query Instructions - - using(SqlConnection conn =NewSqlConnection (connstr)) - { +SqlDataAdapter ada =NewSqlDataAdapter (SELCMDSTR, conn); - +DataSet ds =NewDataSet (); A Ada. Fill (DS); at - if(ds. Tables.count >0) - { -Printdatatable (ds. tables[0]); - } - } in - Console.read (); to } + - /// <summary> the ///Print out the contents of a DataTable * /// </summary> $ Static voidprintdatatable (DataTable DT)Panax Notoginseng { - intCol =dt. Columns.count; the + foreach(DataRow rowinchdt. Rows) A { the for(inti =0; I < col; ++i) + { -Console.Write ("{0}\t", Row[i]); $ } $ } - } - } the -}
Output Result:
3. Summary
In this section, we mainly learn about the constructor of the Adatapter object and how to populate the dataset with data. Adatapter constructors are much more and have distinct characteristics. In real-world development, you can choose a suitable constructor to create a DataAdapter object. As far as my personal experience is concerned, the 2nd and 3rd constructors use more than one. If the query instruction takes no arguments, the 3rd constructor is used, and the 2nd constructor is used if the query directive takes parameters.