2013-12-10 43 views
0

我想從dataGridView1向MySql數據庫添加新值。代碼本身似乎是正確的,在Visual Studio 2012中沒有錯誤,但沒有數據插入到我的數據庫中。 下面是我使用的代碼:將DataGridView值插入到MySql數據庫中。 c#

private void button2_Click(object sender, EventArgs e) 
{ 
    confirm exec = new confirm();  
} 

public class confirm 
{ 
    public void method(DataGridViewCellEventArgs f) 
    { 
     DataGridView dataGridView1 = new DataGridView(); 
     Label label1 = new Label(); // contains User ID which is used for payer_code 
     Label label6 = new Label(); // contains current dayTime 

     foreach (DataGridViewRow row in dataGridView1.Rows) 
     { 
     if ((bool)dataGridView1.Rows[f.RowIndex].Cells["paidDataGridViewTextBoxColumn"].Value == true) 
     { 
      try 
      { 
       string MyConnectionString = "Server=localhost; Database=contractsdb; Uid=root; Pwd="; 
       MySqlConnection connection = new MySqlConnection(MyConnectionString); 
       MySqlCommand cmd = new MySqlCommand(); 
       cmd = connection.CreateCommand(); 
       connection.Open(); 
       cmd.CommandText = "INSERT INTO payments(pay_name, pay_code, payer_code, pay_sum, pay_date)VALUES(@pay_name, @pay_code, @payer_code, @pay_sum, @pay_date)"; 
       cmd.Parameters.AddWithValue("@pay_name", dataGridView1.Rows[f.RowIndex].Cells["contractnameDataGridViewTextBoxColumn"].Value); 
       cmd.Parameters.AddWithValue("@pay_code", dataGridView1.Rows[f.RowIndex].Cells["contractcodeDataGridViewTextBoxColumn"].Value); 
       cmd.Parameters.AddWithValue("@payer_code", label1.Text); 
       cmd.Parameters.AddWithValue("@pay_sum", dataGridView1.Rows[f.RowIndex].Cells["sumDataGridViewTextBoxColumn"].Value); 
       cmd.Parameters.AddWithValue("@pay_date", label6.Text); 
       cmd.ExecuteNonQuery(); 
       connection.Close(); 
      } 

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

可能是它甚至沒有進入'if'檢查 – Shaharyar

+0

嗯,它可能是,但我在這裏輸了...有沒有其他的方式來工作? –

+0

在'connection.Open()'行上設置一個'break point'並檢查它是否到達那裏。我認爲它不是 – Shaharyar

回答

0

我想你是誤會一些關於OOP。像這樣做:

confirm類方法也應該有datagridview1參考(你正在創建一個空datagridview所以它永遠不會甚至進入foreach循環)

public void method(DataGridView datagridview1) //remove your first argument, you don't need it anymore 
{ 
    //delete the line "DataGridView dataGridView1 = new DataGridView();" 
    //and keep the rest of the code as it is 
    foreach (DataGridViewRow row in dataGridView1.Rows) 
    { 
     if(row.Cells["paidDataGridViewTextBoxColumn"].Value == true) //it will check every row, and you don't need "DataGridViewCellEventArgs" argument now 
     { 
      try 
      { 
       //your code, it will be same here 
      } 
     } 
} 

調用的方法:

(使用相同的button_click事件作爲你在做)

private void button2_Click(object sender, EventArgs e) 
{ 
    confirm exec = new confirm();  
    exec.method(datagridview1); //pass "datagridview1" reference 
} 

它會將原始datagridview1的參考文件傳遞給confirm類。

+0

對不起,我是編程新手:)我應該放什麼而不是「yourfirstargument」? –

+0

這是一個事件參數。何時何地調用'method'? – Shaharyar

+0

@ user3041437我已經更新了代碼,希望你現在得到它 – Shaharyar