如何在创建多条记录时记录该值?数据库访问c#

时间:2017-09-11 10:51:34

标签: c# ms-access

else {
    connection.Open();

    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    command.CommandText = "select * from tbl_Checkins where CustomerName='" + textBox1.Text + "'and Gender ='" + textBox2.Text + "'and NumberofPerson ='" + textBox3.Text + "'and Address ='" + textBox5.Text + "'and ContactNumber ='" + textBox6.Text + "'and EmailAddress ='" + textBox7.Text + "'and RoomRate ='" + comboBox4.Text + "'and RoomType ='" + comboBox2.Text + "'and RoomNumber ='" + comboBox3.Text + "'and Checkindate ='" + DatePicker.Text + "'and Checkindate ='" + textBox4.Text + "'";
    OleDbDataReader reader = command.ExecuteReader();

    int count = 0;
    while (reader.Read()){
        count = count + 1;
    }
    if (count > 0){
        MessageBox.Show("Customer already checkin");
    } 
    connection.Close();
    if (count < 1){
        connection.Open();

        command.CommandText = "insert into tbl_Checkins ([CustomerName],[Gender],[Numberofperson],[Address],[ContactNumber],[EmailAddress],[RoomRate],[RoomType],[RoomNumber],[Checkindate],[Checkintime]) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + comboBox4.Text + "','" + comboBox2.Text + "','" + comboBox3.Text + "','" + DatePicker.Text + "','" + textBox4.Text + "')";
        command.ExecuteNonQuery();
        MessageBox.Show("Check IN");
        connection.Close();
    }
}

我想显示客户是否已经办理登机手续。但是,使用上面的代码,会将重复记录添加到数据库中,而不是仅显示信息。

0 个答案:

没有答案