无法在C#数据库访问中更新数据

问题描述 投票:0回答:2

我的按钮3有问题,它是更新按钮,消息框一直说这是更新语句中的语法错误。而且,如果我创建另一个列表框,如果我插入新数据,它不会让我在第二个列表框中插入另一个数据。因此,如果我在第一个列表框中插入一些内容,该索引将是,例如,9,那么我会尝试在下一个列表框中插入,但随后它将继续到索引 10。

    OleDbCommand cmd = new OleDbCommand();
        OleDbConnection cn = new OleDbConnection();
        OleDbDataReader dr;

private void listBox2_Click(object sender, EventArgs e)
        {
            ListBox l = sender as ListBox;

                if (l.SelectedIndex != 1)
                {
                    listBox1.SelectedIndex = l.SelectedIndex;
                    listBox2.SelectedIndex = l.SelectedIndex;
                    textBox2.Text = listBox2.SelectedItem.ToString();
                }

        }

private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                string q = "insert into Table1 (name) values ('"+textBox1.Text.ToString()+"')";
                doSomething(q);
                textBox1.Text = null;
            }
        }

private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                string q = "delete from Table1 where id=" + listBox1.SelectedItem.ToString();
                doSomething(q);
            }
        }

private void button3_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != "" & listBox1.SelectedIndex != -1)
            {
                string q = "update Table1 set (name) '" + textBox2.Text.ToString() + "' where id " + listBox1.SelectedItem.ToString();
                doSomething(q);
                textBox2.Text = "";
            }
        }


private void doSomething(String q)
        {
            try
            {
                cn.Open();
                cmd.CommandText = q;
                cmd.ExecuteNonQuery();
                cn.Close();
                loaddata();
            }
            catch (Exception e)
            {
                cn.Close();
                MessageBox.Show(e.Message.ToString());
            }
        }
c# ms-access listbox
2个回答
1
投票

问题1:在提供输入参数时缺少

=
符号。

试试这个:

 string q = "update Table1 set [name]= '" + textBox2.Text.ToString() + "' where id= " + listBox1.SelectedItem.ToString();

问题2:您没有将连接对象分配给`OleDbCommand。

添加此:在执行命令之前

   cmd.Connection=cn;

完整代码:

        OleDbCommand cmd = new OleDbCommand();
        OleDbConnection cn = new OleDbConnection();
        OleDbDataReader dr;

       private void listBox2_Click(object sender, EventArgs e)
        {
            ListBox l = sender as ListBox;  
            if(l.SelectedIndex!=-1)
            textBox2.Text = l.SelectedItem.ToString();
        }

        }

       private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                string q = "insert into Table1(name) values ('"+textBox1.Text.ToString()+"')";
                doSomething(q);
                textBox1.Text = null;
            }
        }

       private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                string q = "delete from Table1 where id=" + listBox1.SelectedItem.ToString();
                doSomething(q);
            }
        }

       private void button3_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != "" & listBox1.SelectedIndex != -1)
            {
                string q = "update Table1 set [name] ='" + textBox2.Text.ToString() + "' where id =" + listBox1.SelectedItem.ToString();
                doSomething(q);
                textBox2.Text = "";
            }
        }


       private void doSomething(String q)
        {
            try
            {
                cn.Open();
                cmd.CommandText = q;
                cmd.Connection=cn;
                cmd.ExecuteNonQuery();
                cn.Close();
                loaddata();
            }
            catch (Exception e)
            {
                cn.Close();
                MessageBox.Show(e.Message.ToString());
            }
        }

建议:您的查询对

SQL injection attacks
开放,我建议使用
Parameterised Queries
来避免它们。

使用参数化查询:

private void doSomething(String q)
    {
        try
        {
            cn.Open();
            cmd.CommandText = "update Table1 set [name]=@name  where id=@id";
            cmd.Parameters.AddWithValue("@name",textBox2.Text.ToString());
            cmd.Parameters.AddWithValue("@id",listBox1.SelectedItem.ToString());
            cmd.ExecuteNonQuery();
            cn.Close();
            loaddata();
        }
        catch (Exception e)
        {
            cn.Close();
            MessageBox.Show(e.Message.ToString());
        }
    }

0
投票
string q = "update Table1 set (name) '" + textBox2.Text.ToString() + "' where id " + listBox1.SelectedItem.ToString();

在上面的代码(btn3)中你丢失了

id =

编写这样的代码:

string q = "update Table1 set (name) '" + textBox2.Text.ToString() + "' where id=" + listBox1.SelectedItem.ToString();

更新:

My Access查询功能:

public  void ExecuteAccessQuery(string _pQuery)
{
    OleDbConnection con = new OleDbConnection("DatabaseConnectionString");
    OleDbCommand cmd = new OleDbCommand(_pQuery, con);

    if (con.State == System.Data.ConnectionState.Closed)
    {
        con.Open();
    }

    cmd.ExecuteNonQuery();
    con.Close();
}
© www.soinside.com 2019 - 2024. All rights reserved.