在vs2013中不能将所有数据添加到数据库c#中

本文关键字:添加 数据库 数据 vs2013 不能 | 更新日期: 2023-09-27 18:11:14

这是我的项目,我不能添加所有的数据,但表"Re"有数据,当我输入数据请帮助我。

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");
        }        
    }

在vs2013中不能将所有数据添加到数据库c#中

您每次都覆盖cmd.CommandText,因此数据仅插入Re表中。

最简单的方法是在每个cmd.CommandText之后编写cmd.ExecuteNonQuery();并运行您的代码。

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");