日期:2014-05-17  浏览次数:20555 次

OleDbParameterCollection 只接受非空的 OleDbParameter 类型对象,不接受 Int32 对象。
这个是数据库访问
  /// <summary>
  /// 根据产品ID删除数据
  /// </summary>
  /// <param name="id">产品ID</param>
  /// <returns></returns>
  public bool Delete_prod_news_ByID(int id)
  {

  try
  {
  string sqlStr = "delete from prod_news where ID=" + id;
  OleDbCommand cmd = new OleDbCommand(sqlStr, con);
  OleDbParameter par = new OleDbParameter("@ID", OleDbType.Integer);
  par.Value = id;
  cmd.Parameters.Add(id);
  con.Open();
  if (cmd.ExecuteNonQuery() > 0)
  {
  return true;
  }
  return false;
  }
  catch (Exception we)
  {

  throw we;
  }
  finally
  {
  con.Close();
  }


这个是业务处理
 protected void imgBtnDel_Click(object sender, ImageClickEventArgs e)
  {
  int id = Convert.ToInt32(((ImageButton)sender).CommandArgument);
  if (papSer.Delete_prod_news_ByID(id))
  {
  Page.ClientScript.RegisterClientScriptBlock(typeof(string),"","<script>alert('删除成功')</script>");
  }
  else
  {
  Page.ClientScript.RegisterClientScriptBlock(typeof(string), "", "<script>alert('删除失败')</script>");
  }
  }



------解决方案--------------------
只能说你好粗心?

string sqlStr = "delete from prod_news where ID=" + id;
OleDbCommand cmd = new OleDbCommand(sqlStr, con);
//OleDbParameter par = new OleDbParameter("@ID", OleDbType.Integer);
//par.Value = id;
//cmd.Parameters.Add(id);
你sql语句都没添加@ID参数,下面为什么要参数化sql语句,注释掉就行了.

------解决方案--------------------
改成:
string sqlStr = "delete from prod_news where ID=?";
OleDbCommand cmd = new OleDbCommand(sqlStr, con);
OleDbParameter par = new OleDbParameter("?", OleDbType.Integer);
par.Value = id;
cmd.Parameters.Add(par);
con.Open();