2014-07-03 69 views
-3

這個我的項目我不能添加所有的數據,但表「重新」有數據時,我輸入數據,請幫助我。不能添加所有數據到db c#在vs 2013

public partial class Form2 : Form 
{ 

    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Project\c#\DB\db2.mdf;Integrated Security=True;Connect Timeout=30"); 
    SqlCommand cmd = new SqlCommand(); 

    private void Form2_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      con.Open(); 
      MessageBox.Show("connected"); 
     } 
     catch 
     { 
      MessageBox.Show("disconnect"); 
     } 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     String date = dateTimePicker1.Text; 
     String re_date = dateTimePicker2.Text; 
     String re_p = null; 
     String pay = null; 
     //recieve 
     if (radioButton1.Checked) 
     { 
      re_p = "this place"; 
     } 
     else{ 
      if (radioButton2.Checked) 
       re_p = "Fax"; 
      else 
       re_p = "post"; 
     } 
     //Payment 
     if (comboBox1.SelectedIndex == 0) 
      pay = "money"; 
     else if (comboBox1.SelectedIndex == 1) 
       pay = "receipt"; 
      else if (comboBox1.SelectedIndex == 2) 
       pay = "transfer"; 
      else if (comboBox1.SelectedIndex == 3) 
       pay = "unpaid"; 

      //command insert 
      cmd.CommandText = "insert into Profile values('" + ID.Text.Trim() + "','" + date + "','" + name.Text.Trim() + "','" + adr.Text.Trim() + "','" + tel.Text.Trim() + "','" + fax.Text.Trim() + "','" + mail.Text.Trim() + "','" + obj.Text.Trim() + "','" + num.Text.Trim() + "','" + syn.Text.Trim() + "','" + namet.Text.Trim() + "')"; 

      cmd.CommandText = "insert into Data values('" + ID.Text.Trim() + "','" + mo.Text.Trim() + "','" + pro.Text.Trim() + "','" + fat.Text.Trim() + "','" + fiber.Text.Trim() + "','" + fiberh.Text.Trim() + "','" + ash.Text.Trim() + "','" + asha.Text.Trim() + "','" + cal.Text.Trim() + "','" + phos.Text.Trim() + "','" + ndf.Text.Trim() + "','" + adf.Text.Trim() + "','" + adfa.Text.Trim() + "','" + en.Text.Trim() + "','" + sodium.Text.Trim() + "','" + pow.Text.Trim() + "','" + am.Text.Trim() + "','" + tan.Text.Trim() + "','" + uri.Text.Trim() + "','" + me.Text.Trim() + "')"; 

      cmd.CommandText = "insert into Re values('" + ID.Text.Trim() + "','" + re_p + "','" + post.Text.Trim() + "','" + re_date + "','" + recieve.Text.Trim() + "','" + sent.Text.Trim() + "','" + pay + "','" + money.Text.Trim() + "')"; 
      cmd.ExecuteNonQuery(); 
      MessageBox.Show("complete"); 

     }   
    } 
+1

請使用參數代替字符串concatination for sql。什麼布料打破了?並顯示與連接的消息框? –

+2

您將設置'cmd.CommandText'三次,每次覆蓋以前的值。 –

+0

我沒有任何錯誤,我不能將數據添加到表「配置文件」和「數據」 –

回答

0

你每次覆蓋cmd.CommandText,因此數據僅插Re表..

簡單的方法可以每cmd.CommandTextcmd.ExecuteNonQuery();並運行代碼後。

0
switch(comboBox1.SelectedIndex) 
{ 
    case 0: 
     pay = "money"; break; 
    case 1: 
     pay = "receipt"; break; 
    case 2: 
     pay = "transfer"; break; 
    case 3: 
     pay = "unpaid"; break; 
} 

cmd.CommandText = "insert into Profile values(@id, @d,'" + name.Text.Trim() + "','" + adr.Text.Trim() + "','" + tel.Text.Trim() + "','" + fax.Text.Trim() + "','" + mail.Text.Trim() + "','" + obj.Text.Trim() + "','" + num.Text.Trim() + "','" + syn.Text.Trim() + "','" + namet.Text.Trim() + "')"; 
cmd.Parameters.AddWithValue("id", ID.Text.Trim()); 
cmd.Parameters.AddWithValue("d", date); 
// etc 
cmd.ExecuteNonQuery(); 
cmd.CommandText = "insert into Data values('" + ID.Text.Trim() + "','" + mo.Text.Trim() + "','" + pro.Text.Trim() + "','" + fat.Text.Trim() + "','" + fiber.Text.Trim() + "','" + fiberh.Text.Trim() + "','" + ash.Text.Trim() + "','" + asha.Text.Trim() + "','" + cal.Text.Trim() + "','" + phos.Text.Trim() + "','" + ndf.Text.Trim() + "','" + adf.Text.Trim() + "','" + adfa.Text.Trim() + "','" + en.Text.Trim() + "','" + sodium.Text.Trim() + "','" + pow.Text.Trim() + "','" + am.Text.Trim() + "','" + tan.Text.Trim() + "','" + uri.Text.Trim() + "','" + me.Text.Trim() + "')"; 
// clear cmd.parameters and use parameters for the above statement and all next statements 
cmd.ExecuteNonQuery(); 
cmd.CommandText = "insert into Re values('" + ID.Text.Trim() + "','" + re_p + "','" + post.Text.Trim() + "','" + re_date + "','" + recieve.Text.Trim() + "','" + sent.Text.Trim() + "','" + pay + "','" + money.Text.Trim() + "')"; 
cmd.ExecuteNonQuery(); 
MessageBox.Show("complete");