我的查询没有正确执行

本文关键字:执行 查询 我的 | 更新日期: 2023-09-27 18:15:57

这是我的c#代码中的一个方法,应该在特定的按钮点击时执行:

   private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            string connectionString = "Data Source=LPMSW09000012JD''SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            string query = "SELECT Code, Description, Next_Code FROM Liguanea_Lane2 WHERE code LIKE '%" + search.Text + "%'; ";
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                string scode = dr.GetString(dr.GetOrdinal("next_code"));
                textBox2.Text = scode;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        //next description
        try
        {
            string connectionString1 = "Data Source=LPMSW09000012JD''SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
            SqlConnection con1 = new SqlConnection(connectionString1);
            con1.Open();
            string query1 = "SELECT Code, Description, Next_Description FROM Liguanea_Lane2 WHERE code LIKE '%" + search.Text + "%'; ";

            SqlCommand cmd1 = new SqlCommand(query1, con1);
            SqlDataReader dr1 = cmd1.ExecuteReader();
            while (dr1.Read())
            {
                string sdes = dr1.GetString(dr1.GetOrdinal("Next_Description"));
                textBox3.Text = sdes;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        search.ResetText();
        textBox1.Clear();
        search.SelectedIndex = search.SelectedIndex + 1;
        textBox2.Clear();
        textBox3.Clear();
        string connectionString2 = "Data Source=LPMSW09000012JD''SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
        SqlConnection con2 = new SqlConnection(connectionString2);
        con2.Open();
        string query2 = "UPDATE Liguanea_Lane2 SET Update_val= '0' where code = '" + search.Text + "'; ";

    }

}

其中的特定块给出了问题:

   string connectionString2 = "Data Source=LPMSW09000012JD''SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
        SqlConnection con2 = new SqlConnection(connectionString2);
        con2.Open();
        string query2 = "UPDATE Liguanea_Lane2 SET Update_val= '0' where code = '" + search.Text + "'; ";

为了更深入地了解它,它的功能是插入到名为"update_val"的列,这是在我的MSSQL数据库表中。该值是根据称为"search"的组合框的输入插入的。我在MSSQL中运行查询,它起作用了。唯一的区别是,我使用"WHERE"命令指定值,而不是从组合框中接收值。c#的问题在于它根本不更新MSSQL中的表。所以我问我的语法是否错了。

p。是的,我知道应该实现参数化查询,以避免SQL注入。这只是为了我自己的练习。所以在这一点上没有任何评论。

我的查询没有正确执行

要执行更新命令,您需要做的事情更像这样:

using (SqlConnection connection = new SqlConnection(
               connectionstring1)) // You won't need a second connection string if both are the same
    {
        SqlCommand command = new SqlCommand(query2, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }