使用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[]; //删除
pTB.Rows[i].Delete(); //插入
DataRow newRow = pTB.NewRow();
newRow["X"] = "";
newRow["Y"] ="";
pTB.Rows.Add(newRow); //**获得更新DeleteCommand、InsertCommand、UpdateCommand
OdbcCommandBuilder pCommbui = new OdbcCommandBuilder(adapter);
adapter.DeleteCommand = pCommbui.GetDeleteCommand();
adapter.InsertCommand = pCommbui.GetInsertCommand(); adapter.Update(pTB);//将对datatable的全部操作更新至数据库
上一篇:你应该知道的25道Javascript面试题


下一篇:iOS多线程GCD(转)