:'''附近的语法错误。'。'检查每条线但不是

时间:2018-02-04 18:15:09

标签: c#

我有这个令人讨厌的问题(语法不正确','。)我已检查每一行并自行重写(我没有从头开始复制任何内容)

这是我的代码

private void btnadd_Click(object sender, EventArgs e)
    {
        int stock = new int();
        cmd = new SqlCommand("SELECT COUNT(*) FROM Products WHERE Item_Code = @Code, Item_Stock = @Stock", con);
        cmd.Parameters.AddWithValue("@Code", codeadd.Text);
        cmd.Parameters.AddWithValue("@Stock", stock);

        con.Open();
        int result = Convert.ToInt32(cmd.ExecuteScalar()); //Here is the error
        con.Close();
        if (result == 1)
        {
            DataGridViewRow row = new DataGridViewRow();
            DialogResult dialogResult = MessageBox.Show("An Item with BrCode already exists. Do you with to add to the stock?", "Item Manager", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {

                cmd = new SqlCommand("UPDATE Products SET Item_Stock = @Stock WHERE Item_Code=@Item_Code", con);
                cmd.Parameters.Add(new SqlParameter("@Stock", stockadd));
                cmd.Parameters.Add(new SqlParameter("@Item_Code", codeadd.Text));
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                dt.Clear();
                sda = new SqlDataAdapter("Select * From Products", con);
                dt = new DataTable();
                sda.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }

2 个答案:

答案 0 :(得分:3)

问题出在Sql语句中,应该是和

cmd = new SqlCommand("SELECT COUNT(*) FROM Products WHERE Item_Code = @Code and Item_Stock = @Stock", con);

答案 1 :(得分:2)

变化:

WHERE Item_Code = @Code, Item_Stock = @Stock

WHERE Item_Code = @Code AND Item_Stock = @Stock

(或OR,无论如何 - 取决于你是想要结合还是交叉)