(3) C # 's ADO DataSet (DataSet)

Source: Internet
Author: User



The dataset is ADO. The central concept of net. Datasets can be treated as an in-memory database, which is a separate data set that is not dependent on the database. (from Baidu Encyclopedia)


Here's a code to sample the use of the dataset: (The following code is a button's Click event)
1 using(SqlConnection conn =NewSqlConnection ("Data source=pc201507182002\\sqlexpress;initial catalog=123456;integrated security=true"))2  {3 Conn. Open ();4 using(SqlCommand cmd =Conn. CreateCommand ())5  {6Cmd.commandtext ="SELECT * from student where name [email protected]";7Cmd. Parameters.Add (NewSqlParameter ("@name", TextBox1.Text));8SqlDataAdapter da =NewSqlDataAdapter (cmd);9DataSet ds =NewDataSet ();Ten da. Fill (DS); One  ADataTable dt = ds. tables[0]; -DataRowCollection rows =dt. Rows; -  for(inti =0; I < rows. Count; i++) the  { -DataRow dr =Rows[i]; - stringName = (string) dr[0]; - stringName1 = (string) dr[1]; +  -MessageBox.Show ("Name:"+name+"----NAME1:"+name1); +  } A  at  -  } -}




Start with the SqlDataAdapter. The ◇sqldataadapter object acts as a bridge between the dataset and the data. We create a new SqlDataAdapter object Da and pass cmd to it while it is being created. Create a DataSet object DS again. ◇ then at the Da.fill (DS) step, the above SELECT statement is executed, and the query results (a table) are populated into the DS. (DS can be a large data set that can hold multiple tables and add 1 to the original number of tables each time it is populated). The DS is saved in memory. ◇datatable is a virtual table that holds data temporarily. Next create this DataTable object DT, and also the table we just queried, that is, DS. TABLE[0] assigned to DT. That is, the DataTable dt = ds.  Tables[0]; This is a statement where DS. Table[]; There can be many types of parameters in brackets, either indexed or array-based. ◇ Create DataRowCollection object in rows and put dt. Rows to assign to it. This rows contains all the row information in the table. ◇ finally create a DataRow object Dr, the rows are divided into row, that is, the meaning of the line, you can pass row[Index | array]; to get the values of each item. ◇ Note: This is the type of the object to be obtained, which requires (int) row[0]; or (string) row[0]; To enforce the type conversion.summing up, in fact, it is a layered subdivision of the process, first through the SqlDataAdapter to the dataset, and then through the dataset in the refinement to the DataTable, Get all the DataRowCollection from the DataTable, and then assign each DataRow, and finally navigate to a column item of the row via the DataRow's object Dr Dr[0]; To obtain a specific item.


(3) C # 's ADO DataSet (DataSet)


Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.