2017-05-09 110 views
0

我有問題試圖找出如何使用文本框和按鈕更新和刪除數據庫內的數據。刪除和更新數據庫c#

我插入的代碼是:

private void button4_Click(object sender, EventArgs e) 
    { 
     try 
     { 



      string query = "insert into Prodaja values (@Prodajalec,@Prodajalna,@Kupec,@Vozilo,@DatumNakupa)"; 

      using (connection = new SqlConnection(connectionString)) 
      using (SqlCommand command = new SqlCommand(query, connection)) 
      { 

       connection.Open(); 
       command.Parameters.AddWithValue("@Prodajalec", textBoxProdajalec.Text); 
       command.Parameters.AddWithValue("@Prodajalna", textBoxProdajalna.Text); 
       command.Parameters.AddWithValue("@Kupec", textBoxKupec.Text); 
       command.Parameters.AddWithValue("@Vozilo", textBoxVozilo.Text); 
       command.Parameters.AddWithValue("@DatumNakupa", textBoxDatumNakupa.Text); 
       command.ExecuteNonQuery(); 



      } 

      MessageBox.Show("Uspešno dodano");    
      this.prodajaTableAdapter.Fill(this.prodajaDataSet.Prodaja); 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

我嘗試了幾個不同的方法,但他們沒有真正的工作了。任何幫助,將不勝感激!

+1

什麼輸出/錯誤信息你收到? –

回答

1

您似乎在混淆插入語句與更新語句的語法。

Insert statement: 
insert into #table (col1,col2) values ('col1','col2') 

Update statement 
Update #table set col1 = 'col1', col2 = 'col2' where id = 'id' 

編輯:刪除語法

Delete from #table where id = 'id' 
+0

是的,我修復了,現在我複製了錯誤的代碼。代碼發佈工作,但我不知道如何更新和從這個基地刪除。 –

+0

您應該只能使用上面的示例語法進行更新。只需根據某些或其他獨特字段添加特定記錄的位置即可。將使用刪除語法更新答案。 – ThatChris

相關問題