Ошибка при изменении записи
Код:
private void bindingNavigatorSaveItem_Click(object sender, EventArgs e
{
OleDbParameter p;
// insert
string strIns = "INSERT INTO tbl1(id, txt) VALUES (?, ?)";
OleDbCommand insertCmd = new OleDbCommand(strIns, con);
da.InsertCommand = insertCmd;
p = new OleDbParameter("@id", OleDbType.Integer);
p.SourceColumn = "id";
p.SourceVersion = DataRowVersion.Original;
insertCmd.Parameters.Add(p);
p = new OleDbParameter("@txt", OleDbType.VarChar);
p.SourceColumn = "txt";
p.SourceVersion = DataRowVersion.Original;
insertCmd.Parameters.Add(p);
// update
string strUpd = "UPDATE tbl1 SET id = ?, txt = ?";
//string strUpd = "UPDATE tbl1 SET txt = ?";
OleDbCommand updateCmd = new OleDbCommand(strUpd, con);
da.UpdateCommand = updateCmd;
p = new OleDbParameter("@id", OleDbType.Integer);
p.SourceColumn = "id";
p.SourceVersion = DataRowVersion.Original;
updateCmd.Parameters.Add(p);
p = new OleDbParameter("@txt", OleDbType.VarChar);
p.SourceColumn = "txt";
p.SourceVersion = DataRowVersion.Original;
updateCmd.Parameters.Add(p);
// delete
string strDel = "DELETE FROM tbl1 WHERE id = ? AND txt = ?";
OleDbCommand deleteCmd = new OleDbCommand(strDel, con);
da.DeleteCommand = deleteCmd;
p = new OleDbParameter("@id", OleDbType.Integer);
p.SourceColumn = "id";
p.SourceVersion = DataRowVersion.Original;
deleteCmd.Parameters.Add(p);
p = new OleDbParameter("@txt", OleDbType.VarChar);
p.SourceColumn = "txt";
p.SourceVersion = DataRowVersion.Original;
deleteCmd.Parameters.Add(p);
da.Update(ds.Tables[0]);
}
{
OleDbParameter p;
// insert
string strIns = "INSERT INTO tbl1(id, txt) VALUES (?, ?)";
OleDbCommand insertCmd = new OleDbCommand(strIns, con);
da.InsertCommand = insertCmd;
p = new OleDbParameter("@id", OleDbType.Integer);
p.SourceColumn = "id";
p.SourceVersion = DataRowVersion.Original;
insertCmd.Parameters.Add(p);
p = new OleDbParameter("@txt", OleDbType.VarChar);
p.SourceColumn = "txt";
p.SourceVersion = DataRowVersion.Original;
insertCmd.Parameters.Add(p);
// update
string strUpd = "UPDATE tbl1 SET id = ?, txt = ?";
//string strUpd = "UPDATE tbl1 SET txt = ?";
OleDbCommand updateCmd = new OleDbCommand(strUpd, con);
da.UpdateCommand = updateCmd;
p = new OleDbParameter("@id", OleDbType.Integer);
p.SourceColumn = "id";
p.SourceVersion = DataRowVersion.Original;
updateCmd.Parameters.Add(p);
p = new OleDbParameter("@txt", OleDbType.VarChar);
p.SourceColumn = "txt";
p.SourceVersion = DataRowVersion.Original;
updateCmd.Parameters.Add(p);
// delete
string strDel = "DELETE FROM tbl1 WHERE id = ? AND txt = ?";
OleDbCommand deleteCmd = new OleDbCommand(strDel, con);
da.DeleteCommand = deleteCmd;
p = new OleDbParameter("@id", OleDbType.Integer);
p.SourceColumn = "id";
p.SourceVersion = DataRowVersion.Original;
deleteCmd.Parameters.Add(p);
p = new OleDbParameter("@txt", OleDbType.VarChar);
p.SourceColumn = "txt";
p.SourceVersion = DataRowVersion.Original;
deleteCmd.Parameters.Add(p);
da.Update(ds.Tables[0]);
}
Это значит что не нужно включать его в качестве параметра.
Код:
string strUpd = "UPDATE tbl1 SET txt = ? WHERE id = ?";
OleDbCommand updateCmd = new OleDbCommand(strUpd, con);
da.UpdateCommand = updateCmd;
p = new OleDbParameter("@txt", OleDbType.VarChar);
p.SourceColumn = "txt";
p.SourceVersion = DataRowVersion.Current;
updateCmd.Parameters.Add(p);
p = new OleDbParameter("@id", OleDbType.Integer);
p.SourceColumn = "id";
p.SourceVersion = DataRowVersion.Original;
updateCmd.Parameters.Add(p);
OleDbCommand updateCmd = new OleDbCommand(strUpd, con);
da.UpdateCommand = updateCmd;
p = new OleDbParameter("@txt", OleDbType.VarChar);
p.SourceColumn = "txt";
p.SourceVersion = DataRowVersion.Current;
updateCmd.Parameters.Add(p);
p = new OleDbParameter("@id", OleDbType.Integer);
p.SourceColumn = "id";
p.SourceVersion = DataRowVersion.Original;
updateCmd.Parameters.Add(p);