OleDbConnection cnn = new OleDbConnection(sCnn);
cnn.Open();
DataTable table = new DataTable();
try
{
OleDbCommand cmd = new OleDbCommand(sCommand, cnn);
cmd.CommandType = cmdType;
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
table = new DataTable(sDataTableName);
adapter.Fill(table);
cnn.Close();
table.PrimaryKey = new DataColumn[1] { table.Columns[0] };
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
builder.QuotePrefix = "[";
builder.QuoteSuffix = "]";
adapter.InsertCommand = builder.GetInsertCommand();
adapter.DeleteCommand = builder.GetDeleteCommand();
adapter.UpdateCommand = builder.GetUpdateCommand();
adapter.RowUpdated += new OleDbRowUpdatedEventHandler(OnRowUpdated);
}
catch
{
}
protected void OnRowUpdated(object sender, OleDbRowUpdatedEventArgs e)
{
if ((e.Status == UpdateStatus.Continue) && e.StatementType == StatementType.Insert)
{
int newID = 0;
OleDbCommand cmdGetId = new OleDbCommand("SELECT @@IDENTITY", e.Command.Connection);
newID = (int)cmdGetId.ExecuteScalar();
e.Row["id"] = newID;
if (newID == 0)
{
MessageBox.Show("获取ID值错误!");
}
}
}
System.Data.DataTable tblChange = table.GetChanges();
affectRowCount = adapter.Update(tblChange);
if (mode == modeNew)
{
row["id"] = tblChange.Rows[0]["id"];
}
table.AcceptChanges();