http://blog.csdn.net/nidexuanzhe/article/details/8228832
说明:通常我们在做数据库交互时,并不一定要使用特定的SQL语句来更新数据,.NET Framwork为我们提供了自动更新的功能
- public static void UpdateTable()
- {
- SqlConnection conn = null;
- string sql = "select *From Course";
-
- DataTable dt = null;
- DataSet ds = new DataSet();
-
- try
- {
- conn = new SqlConnection(connectionString);
- SqlDataAdapter sda = new SqlDataAdapter();
- sda.SelectCommand = new SqlCommand(sql, conn);
- SqlCommandBuilder cb = new SqlCommandBuilder(sda);
-
- conn.Open();
-
- sda.Fill(ds);
- dt = ds.Tables[0];
-
- DataRow dr = dt.NewRow();
- dr["ID"] = 1006;
- dr["Name"] = "面向对象编程";
- dr["Grade"] = "10004";
- dt.Rows.Add(dr);
-
- sda.Update(dt);
-
-
-
- dt.AcceptChanges();
- }
- catch (SqlException ex)
- { }
- finally
- {
- conn.Close();
- }
- }
对DataTable(或者DataSet)修改后,提交修改到数据库
原文:http://www.cnblogs.com/LuoEast/p/7833897.html