简化这样的操作: http://blog.csdn.net/greatverve/archive/2009/03/02/3948648.aspx/****************CommandBuilder简化sql语句的生成****************** * ****************************************************************/ //DataAccess.cs中增加如下方法 /// <summary> /// 使用CommandBuilder自动生成对应的sql语句 /// </summary> /// <param name="ds"></param> /// <param name="tableName"></param> public static void UpdateDataSetByCommandBuilder(DataSet ds, string tableName) { OleDbConnection conn = new OleDbConnection(strCon); conn.Open(); //显示原始数据 OleDbDataAdapter da = new OleDbDataAdapter("select * from workerInf", conn); OleDbCommandBuilder cb = new OleDbCommandBuilder(da); da.Update(ds, tableName); conn.Close(); } //DataForm.cs中修改代码如下: protected void InsertRecord() { try { ds.Tables["workerInf"].Rows[myBind.Position].BeginEdit(); //构造DataRow DataRow dr = ds.Tables["workerInf"].NewRow(); dr["name"] = "hello"; dr["sex"] = "男"; ds.Tables["workerInf"].Rows.Add(dr); ds.Tables["workerInf"].Rows[myBind.Position].EndEdit(); //更新数据库,简化多了 AppCode.DataAccess.UpdateDataSetByCommandBuilder(ds, "workerInf"); ds.Tables["workerInf"].AcceptChanges(); } catch (Exception ed) { MessageBox.Show("增加数据记录发生 " + ed.ToString(), "错误!"); } }