2016-02-26 108 views
-3
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說錯誤「的關鍵字附近有語法錯誤,其中」

你們可以幫我「關鍵字‘其中’附近有語法錯誤」這件事?

+1

這是什麼版本的SQL?微軟SQL? [Insert語句](https://msdn.microsoft.com/en-us/library/ms174335.aspx?f=255&MSPPError=-2147217396)沒有where子句。 –

+0

您的註釋行更接近正確的語法(儘管您應該使用參數)。 –

+0

你的錯誤是在你的查詢語句中,它與out插入到表中,你正在設置語句 –

回答

2
string Query = 
"INSERT INTO Produtos (id_subcategoria, nome_produto, quantidade, preco_unitario, iva) 
VALUES (@id_subcategoria, @nome_produto, @quantidade, @preco_unitario, @iva)"; 
相關問題