使用c#在访问中未提交的数据

时间:2014-10-27 06:05:30

标签: c#

try {
    if (connection.con.State == ConnectionState.Closed) connection.con.Open();
    com = new OleDbCommand("insert into dealer values(@id,@a1,@a2,@a3,@a4,@a5,@a6)", connection.con);
    com.Parameters.AddWithValue("@a1", int.Parse(t1.Text));
    com.Parameters.AddWithValue("@a2", t2.Text);
    com.Parameters.AddWithValue("@a3", t3.Text);
    com.Parameters.AddWithValue("@a4", t4.Text);
    com.Parameters.AddWithValue("@a5", t5.Text);
    com.Parameters.AddWithValue("@a6", t6.Text);
    com.Parameters.AddWithValue("@a7", c1.Text);
    int i = com.ExecuteNonQuery();
    if (i > 0) {
        MessageBox.Show("dealer Registration Seccessfully !!");
    }
} catch (Exception ex) {
    MessageBox.Show(ex.ToString());
} finally {
    connection.con.Close();
}

插入数据库的数据显示在数据库中,但有些数据会自动删除

1 个答案:

答案 0 :(得分:0)

显然你的代码中有一个拼写错误,如果表的Id不是自动增加,只需更正参数名称:

try {
    if (connection.con.State == ConnectionState.Closed) connection.con.Open();
    com = new OleDbCommand("insert into dealer values(@id,@a1,@a2,@a3,@a4,@a5,@a6)", connection.con);
    com.Parameters.AddWithValue("@id", int.Parse(t1.Text)); // Change argument name to @id
    com.Parameters.AddWithValue("@a1", t2.Text);
    com.Parameters.AddWithValue("@a2", t3.Text);
    com.Parameters.AddWithValue("@a3", t4.Text);
    com.Parameters.AddWithValue("@a4", t5.Text);
    com.Parameters.AddWithValue("@a5", t6.Text);
    com.Parameters.AddWithValue("@a6", c1.Text);
    ...

如果表的Id是自动递增,则不要在INSERT命令中设置其值。