C# :“靠近”“的语法不正确。”选中了everyline,但没有
我有一个恼人的问题,语法不正确,新的','。我检查了每一行并自己重写了它。我从一开始就没有复制任何东西 这是我的密码C# :“靠近”“的语法不正确。”选中了everyline,但没有,c#,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 = @S
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;
}
}
更改:
WHERE Item_Code = @Code, Item_Stock = @Stock
到
或者,或者,不管怎样-取决于您是在尝试联合还是相交问题出在Sql语句中,应该是和
cmd = new SqlCommand("SELECT COUNT(*) FROM Products WHERE Item_Code = @Code and Item_Stock = @Stock", con);
如果知道这是编译错误还是运行时SQL异常,那将非常有用。。。始终准确地发布整个错误,即使是您不理解的部分。有关更多信息,请推荐我为什么否决投票?您将问题标记为C,但这在SQL中是个问题。这是一个很大的区别。因为我认为问题在于C代码而不是SQL。。。
cmd = new SqlCommand("SELECT COUNT(*) FROM Products WHERE Item_Code = @Code and Item_Stock = @Stock", con);