日期:2014-05-18  浏览次数:20459 次

cmd.ExecuteNonQuery()出错,求教
protected void Button1_Click(object sender, EventArgs e)
  {
  try
  {
  OleDbConnection dbconn = connection.geton();
  OleDbDataAdapter da = new OleDbDataAdapter(@"select * from userinfo", dbconn);
  DataSet ds = new DataSet(); 
  da.Fill(ds, "userinfo");
  bool n = true;
  for (int m = 0; m < ds.Tables[0].Rows.Count; m++)
  {
  if (ds.Tables[0].Rows[m][0].ToString() == TextBox1.Text)
  {

  n = false;

  }
  }
  if (n)
  {
  OleDbCommand cmd = new OleDbCommand("INSERT INTO userinfo(name,password) values(@name,@password)", dbconn);
  OleDbParameter nameparam = new OleDbParameter("@name", TextBox1.Text);
  OleDbParameter passwordparam = new OleDbParameter("@password", TextBox2.Text);
  cmd.Parameters.Add(nameparam);
  cmd.Parameters.Add(passwordparam);
  int j = cmd.ExecuteNonQuery();
  if (j > 0)
  Response.Write("<script laguage='javascript'>alert('注册成功');</script>");
  else
  Response.Write("<script laguage='javascript'>alert('注册失败');</script>");
  dbconn.Close();
  }
  else
  {
  Response.Write("<script laguage='javascript'>alert('此用户名已被注册');</script>");
  TextBox1.Text = "";
  }
  }
  catch(Exception ex)
  {
  Response.Write(ex.ToString());

  }
   
  }


错误提示
System.Data.OleDb.OleDbException (0x80040E14): INSERT INTO 语句的语法错误。 在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) 在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) 在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) 在 System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) 在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) 在 System.Data.OleDb.OleDbCommand.ExecuteNonQuery() 在 csharp.Register.Button1_Click(Object sender, EventArgs e) 位置 F:\程序\csharp\Backup\Backup\Backup\csharp\Register.aspx.cs:行号 46

------解决方案--------------------
可能应该这样 INSERT INTO userinfo([name],[password]) values(@name,@password)
还有 dbconn似乎没有open
------解决方案--------------------
protected void Button1_Click(object sender, EventArgs e)
{
try
{
OleDbConnection dbconn = connection.geton();
OleDbDataAdapter da = new OleDbDataAdapter("select * from userinfo", dbconn); //画蛇添足,去掉@
DataSet ds = new DataSet(); 
da.Fill(ds, "userinfo");
bool n = true;
for (int m = 0; m < ds.Tables[0].Rows.Count; m++)
{
if (ds.Tables[0].Rows[m][0].ToString() == TextBox1.Text)