『壹』 C# datatable 讀取資料庫
DataTable dtb = null;
string sql = "select class from table1 where name = '張三' ";
dtb = db.GetDataTable(db.form,sql);
string dtbStr=dtb.Rows[行下標][列下標].toString(); //兩種寫法,獲得datatable中的某行某列專的值
string dtbStr1=dtb.Rows[行下標]["列名屬"].toString();
『貳』 如何將DataTable更新到資料庫中
SqlDataAdapter adapter = new SqlDataAdapter(strSql, strConn);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
DataSet ds = new DataSet();
adapter.Fill(ds, "Titles");
//插入記錄
DataTable table = ds.Tables["Titles"];
DataRow row = table.NewRow();
row["Title_id"] = "JP1001";
row["title"] = "programming Microsoft .NET";
row["price"] = 59.99m;
row["ytd_sales"] = 100000;
row["type"] = "business";
row["pubdate"] = new DateTime(2002, 5, 1);
table.Rows.Add(row);
//更新資料庫
adapter.Update(table);