Snail ‑c # DataAdapter object

Source: Internet
Author: User

Use DataAdapter and DataSet to read data in the data table JBQK

Using System; using System. collections. generic; using System. linq; using System. text; using System. data. sqlClient; using System. data; namespace shiyan11 {class Program {static void Main (string [] args) {string strCon = @ "Data Source =. \ SQLEXPRESS; Initial Catalog = TestDB; Integrated Security = True; "; SqlConnection sqlCon = new SqlConnection (strCon); try {sqlCon. open (); string sqlStr = @ "select No, Name, Gra De from JBQK "; SqlCommand sqlCmd = new SqlCommand (sqlStr, sqlCon); SqlDataAdapter sda = new SqlDataAdapter (sqlCmd); DataSet ds = new DataSet (); sda. fill (ds); // Fill the dataset. The essence is to Fill the 0th tables in ds with string sltResult = ""; DataTable dt = ds. tables [0]; Console. writeLine ("basic data Table query results:"); for (int I = 0; I <dt. rows. count; I ++) {// read data row by row. Each row accesses sltResult + = "nth" + (I + 1) + "record:" + dt. rows [I] [0]. toString () + "\ t "+ Dt. rows [I] ["Name"]. toString () + dt. rows [I] [2]. toString () + "\ n";} Console. writeLine (sltResult);} catch (Exception e) {Console. writeLine ("failed !! ") ;}Sqlcon. Close (); Console. Read ();}}}

Delete the first data in the JBQK table

Using System; using System. collections. generic; using System. linq; using System. text; using System. data. sqlClient; using System. data; namespace shiyan11 {class Program {static void Main (string [] args) {string strCon = @ "Data Source =. \ SQLEXPRESS; Initial Catalog = TestDB; Integrated Security = True; "; SqlConnection sqlCon = new SqlConnection (strCon); try {sqlCon. open (); string sqlStr = @ "select No, Name, Grade from JBQK"; SqlCommand sqlCmd = new SqlCommand (sqlStr, sqlCon); SqlDataAdapter sda = new SqlDataAdapter (sqlCmd ); dataSet ds = new DataSet (); sda. fill (ds); // Fill the dataset. The essence is to Fill the 0th tables in ds with string sltResult = ""; // ------------------------------------------ // initialize SqlCommandBuilder strength SqlCommandBuilder scb = new SqlCommandBuilder (sda) with sda as the parameter; // Delete the first row of Data ds in JBQK of DataSet. tables [0]. rows [0]. delete (); // call the Update method to Update the database sda with data in DataSet. update (ds, ds. tables [0]. toString (); ds. tables [0]. acceptChanges (); // ------------------------------------------------ DataTable dt = ds. tables [0]; Console. writeLine ("basic data Table query results:"); for (int I = 0; I <dt. rows. count; I ++) {// read data row by row. Each row accesses sltResult + = "nth" + (I + 1) + "record:" + dt. rows [I] [0]. toString () + "\ t" + dt. rows [I] ["Name"]. toString () + dt. rows [I] [2]. toString () + "\ n";} Console. writeLine (sltResult);} catch (Exception e) {Console. writeLine (e. toString ();} sqlCon. close (); Console. read ();}}}

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.