-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");
}
}
請使用參數代替字符串concatination for sql。什麼布料打破了?並顯示與連接的消息框? –
您將設置'cmd.CommandText'三次,每次覆蓋以前的值。 –
我沒有任何錯誤,我不能將數據添加到表「配置文件」和「數據」 –