使用DataTable更新数据库

 

简例:

string connStr = string.Format("Driver={1}Microsoft Access Driver (*.mdb){2};DBQ={0};", mdbPath, "{", "}");//连接本地mdb
OdbcConnection m_OdbcConn = new OdbcConnection(connStr);
m_OdbcConn.Open();

string sql = string.Format("select ID,X,Y from {0}", pTableName);//**主键一定要选择
OdbcDataAdapter adapter = new OdbcDataAdapter(sql, m_OdbcConn);
DataSet dt = new DataSet();
adapter.Fill(dt);
m_OdbcConn.Close();
DataTable pTB = dt.Tables[0];

//删除
pTB.Rows[i].Delete();

//插入
DataRow newRow = pTB.NewRow();
newRow["X"] = "1312";
newRow["Y"] ="1231";
pTB.Rows.Add(newRow);

//**获得更新DeleteCommand、InsertCommand、UpdateCommand
OdbcCommandBuilder pCommbui = new OdbcCommandBuilder(adapter);
adapter.DeleteCommand = pCommbui.GetDeleteCommand();
adapter.InsertCommand = pCommbui.GetInsertCommand();

adapter.Update(pTB);//将对datatable的全部操作更新至数据库

 

posted @ 2016-12-19 15:43  Hxxxxxxyyyyyy  阅读(2503)  评论(0编辑  收藏  举报