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 ();}}}