2015-04-25 76 views
0

我正在测试我的程序,以及何时执行了vs vs没有任何错误执行! 这是我的代码:安装应用程序后出现错误插入查询

private void button1_Click(object sender, EventArgs e) 
     { 
      OleDbConnection conect = new OleDbConnection(); 
      conect.ConnectionString = "provider=microsoft.jet.oledb.4.0;" + "data source=university.mdb;Jet OLEDB:Database [email protected]"; 
      conect.Open(); 
      OleDbCommand o1 = new OleDbCommand(); 
      o1.Connection = conect; 
      if(button1.Text=="save") 
       o1.CommandText = "insert into check_user(name_user,pw_user)values('" + textBox1.Text + "','" + textBox2.Text + "')"; 
      else 
       o1.CommandText = " select * from check_user WHERE (name_user = '" + textBox1.Text + "') and (pw_user = '" + textBox2.Text + "')"; 
      o1.ExecuteNonQuery(); 

      if (button1.Text != "save") 
      { 
       if (o1.ExecuteScalar() == null) 
        MessageBox.Show("wrong user"); 
       else 
       { 
        groupBox1.Visible = false; 
        menuStrip1.Visible = true; 
       } 
      } 
      else 
      { 
       groupBox1.Visible = false; 
       menuStrip1.Visible = true; 
      } 

      conect.Close(); 
     } 

但时将执行安装后的应用程序和运行发生此查询错误: http://s4.picofile.com/file/8184692692/qq.png 任何查询选择的执行没有错误,但查询插入或删除出现此错误 请帮我

+0

检查连接字符串.. –

+0

连接没有任何问题。在测试vs时,任何错误都不存在,但是在运行时安装错误发生在查询插入和删除 –

回答

1

您不能将NonQuery与“Select”一起使用。试试这个

if(button1.Text=="save") 
{ 
    o1.CommandText = "insert into check_user(name_user,pw_user)values('" + textBox1.Text + "','" + textBox2.Text + "')"; 
    o1.ExecuteNonQuery(); 
} 
else 
{ 
    o1.CommandText = " select * from check_user WHERE (name_user = '" + textBox1.Text + "') and (pw_user = '" + textBox2.Text + "')"; 
    o1.ExecuteQuery(); 
}​ 
+0

即使在这种情况下,也会发生错误。 查询选择没有任何错误执行,但查询插入或删除后安装此错误发生 –

+0

oledb不支持字段的名称,所以你必须使用像下面的网页上的参数:http://stackoverflow.com/questions/15910977 /插入-成存取数据库 – jdweng

相关问题