安装应用程序后插入错误

时间:2015-04-25 06:35:24

标签: c#

我正在测试我的程序,并在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 Password=sa@a";
            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();
        }

但是在安装app之后执行并运行此查询时会发生错误: http://s4.picofile.com/file/8184692692/qq.png 任何查询选择没有错误执行但查询插入或删除发生此错误 请帮帮我

1 个答案:

答案 0 :(得分:1)

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

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();
}​