2015/07/08 using system;using system.collections.generic;using system.linq;using system.text;using System.threading.tasks;using system.data;using System.data.sqlclient;namespace DataTapleSample{class Program { static void Main (string[] args) {//generic dedicated to save the type of data in the database DataTable table = new DataTable (); Before you can save data in a DataTable, you must first define the structure DataColumn Stuid =//new DataColumn ("Stuid", System.typ E.gettype ("System.Int32")); New DataColumn ("Stuid", typeof (int));//shorthand and the former equivalent DataColumn stuname = new DataColumn ("Stuname", typeof (String)); DataColumn stuaddress = new DataColumn ("Stuaddress", typeof (String)); Create the table's structure table. Columns.Add (STUID); Table. Columns.Add (Stuname); Table. Columns.Add (stuaddress); Table constraint//PRIMARY KEY constraint table. PrimaryKey = new Datacolumn[]{stuid}; Non-null constraint//stuaddress. AllowDBNull = false; Unique constraint: Stuname. Unique; How to save data in a DataTable//DataRow represents a row of data saved in a DataTable. DataRow row = table. NewRow (); Rows created using the NewRow method have the same structure as the table (the following three methods, recommended first)//row[stuid] = 1; ROW[1] = "XXXX"; Row[stuaddress] = "XXXX"; Row[stuid] = 7; Row[stuname] = "XX"; Row[stuaddress] = "XXX"; Now join the table in the DataTable. Rows.Add (row); Accesses the data stored in the DataTable foreach (DataRow r in table. Rows) {Console.WriteLine ("Stuid:{0},stuname:{1},stuaddress:{2}", r[0],r[1],r[2]); } console.readkey (); }}}////////////////////////////////////////////////////////////////next//2015/07/08//DataTable && Sqldatareaderusing system;using system.collections.generic;using system.linq;using System.Text;using System.threading.tasks;using system.data;using System.Data.SqlCLient;namespace studatatable{class Program {static void Main (string[] args) {//saved in Data Table in DataTable table = new DataTable (); String connectionString = "server=.; database=booksample;uid=sa;pwd=123456 "; using (SqlConnection connection = new SqlConnection (connectionString)) {String sql = "Select ID , Stuname,phone from students "; SqlCommand cmd = new SqlCommand (sql,connection); Connection. Open (); using (SqlDataReader reader = cmd. ExecuteReader ()) {//based on the structure of the query result, create the corresponding DataTable int columnCount = RE Ader. FieldCount; Number of columns of the query result//column created for (int i = 0; i < ColumnCount; i++) { DataColumn column = new DataColumn (reader. GetName (i), reader. GetFieldType (i) ); Table. Columns.Add (column); }//row-by-line reading of data from the database while (reader. Read ()) {DataRow row = table. NewRow (); for (int i = 0; i < ColumnCount; i++) {row[i] = Reader[i]; } table. Rows.Add (row); }}}//Now the data in the database has been saved to a special collection in memory foreach (DataRow Row in tab Le. Rows) {//ID read out Console.WriteLine (row["id"]); } console.readkey (); }}}/* Related reading: Https://msdn.microsoft.com/zh-cn/library/system.data.datatable.aspx https://msdn.microsoft.com/zh-cn/ library/system.data.sqlclient.sqldatareader.aspx * *
DataTable && SqlDataReader help understand small programs