c#和db编程很新。我已经接管了其他人的代码。我正在尝试更新数据库时遇到错误。这是代码:
private void EnableEvent(int eventID)
{
OleDbCommand oleCMD = new OleDbCommand();
oleCMD.Connection = Database.SqlConn();
OleDbTransaction oleTrans = oleCMD.Connection.BeginTransaction();
oleCMD.Transaction = oleTrans;
try
{
StringBuilder sql = new StringBuilder();
sql.AppendFormat("UPDATE Events SET isActive = 1 where EventID='{0}'", eventID);
oleCMD.CommandText = sql.ToString();
// insert the header
oleCMD.ExecuteNonQuery();
oleTrans.Commit();
}
catch(Exception e)
{
MessageBox.Show(e.Message, "Database Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
oleCMD.Connection.Close();
oleCMD.Dispose();
}
}
答案 0 :(得分:0)
修改强>
现在问题...如下所示格式化您的查询,它肯定会起作用
StringBuilder sql = new StringBuilder();
sql.AppendFormat("UPDATE Events SET isActive = 1 where EventID = {0}", eventID);
对于Numeric
字段,您不应该在值周围放置撇号。这就是问题所在。
答案 1 :(得分:0)
试试这个!
private void EnableEvent(int eventID)
{
OleDbConnection myConn = new OleDbConnection(myConnString);
myConn.Open();
OleDbCommand myCommand = myConn.CreateCommand();
OleDbTransaction myTrans;
// Start a local transaction
myTrans = myConn.BeginTransaction();
// Assign transaction object for a pending local transaction
myCommand.Connection = myConn;
myCommand.Transaction = myTrans;
try
{
StringBuilder sql = new StringBuilder();
sql.AppendFormat("UPDATE Events SET isActive = 1 where EventID='{0}'", eventID);
myCommand.CommandText = sql.ToString();
// insert the header
myCommand.ExecuteNonQuery();
myTrans.Commit();
}
catch(Exception e)
{
MessageBox.Show(e.Message, "Database Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
myCommand.Connection.Close();
myCommand.Dispose();
}
}
请告诉我这是否有帮助!