【发布时间】:2016-02-26 22:02:32
【问题描述】:
private void btnguardar_Click(object sender, EventArgs e)
{
try
{
con.Open();
string Query = "INSERT Produtos where (id_subcategoria= @id_subcategoria, nome_produto= @nome_produto, quantidade= @quantidade, preco_unitario= @preco_unitario, iva= @iva)";
SqlCommand createCommand = new SqlCommand(Query, con);
createCommand.Parameters.AddWithValue("@id_subcategoria", this.label4.Text);
createCommand.Parameters.AddWithValue("@nome_produto", this.txt_nproduto.Text);
createCommand.Parameters.AddWithValue("@quantidade", this.txtquantidade.Text);
createCommand.Parameters.AddWithValue("@preco_unitario", Convert.ToDecimal(this.txtpreco.Text));
createCommand.Parameters.AddWithValue("@iva", Convert.ToDecimal(this.txtiva.Text));
createCommand.ExecuteNonQuery();
MessageBox.Show("Registo adicionado com sucesso!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
//"insert into Produtos (id_subcategoria, nome_produto, quantidade, preco_unitario, iva) values('" + this.label4.Text + "','" + this.txt_nproduto.Text + "','" + this.txtquantidade.Text + "','" + this.txtpreco.Text + "','" + this.txtiva.Text + "') ;";
}
我在点击 btnguardar 时遇到错误,并打开 MessageBox 说“关键字 'where' 附近的语法不正确”
你们能帮我解决这个问题吗?
【问题讨论】:
-
这是什么版本的 SQL?微软 SQL? Insert statement 没有 where 子句。
-
您注释掉的行更接近正确的语法(尽管您应该使用参数)。
-
你的错误是在你的查询语句中,它没有插入到表中,你正在设置 out into 语句
标签: c# database syntax-error