Not an IT professional, syntax is wrong, it just shows what I would like to do. Now how to write the code correctly?
Access database table TableAppointment has three fields: Username, AppointmentDate, AppointmentNote
dateTimePicker1.Value <= dateTimePicker2.Value, which has been checked.
Now, I would like to delete records WHERE Username = Environment.UserName AND AppointmentDate AND AppointmentDate >= dateTimePicker1.Value.Date AND AppointmentDate <= dateTimePicker2.Value.Date
Then I INSERT records based on same date range (ApointmentDate >= dateTimePicker1.Value.Date AND AppointmentDate <= dateTimePicker2.Value.Date) and textbox input. Maybe I only need to insert Date into database, no need to insert time into database.
Thanks.
Edit: I have set Access date format as yyyy-mm-dd
for (DateTime dt = dateTimePicker1.Value; dt <= dateTimePicker2.Value; dt=dt.AddDays(1))
{
using (OdbcConnection Cn = new OdbcConnection(GlobalVariables.DatabaseConnectionString)) //Access database
{
//Access database table TableAppointment has three fields: Username, AppointmentDate, AppointmentNote
//dateTimePicker1.Value <= dateTimePicker2.Value, which has been checked.
string sqlDelete = "DELETE FROM TableAppointment WHERE Username = '" + Environment.UserName + "' AND AppointmentDate >= dateTimePicker1.Value.Date AND AppointmentDate <= dateTimePicker2.Value.Date";
OdbcCommand cmdDelete = new OdbcCommand(sqlDelete, Cn);
Cn.Open();
cmdDelete.ExecuteNonQuery();
}
using (OdbcConnection Cn = new OdbcConnection(GlobalVariables.DatabaseConnectionString)) //Access database
{
//Access database table TableAppointment has three fields: Username, AppointmentDate, AppointmentNote
string sqpInsert = "INSERT INTO TableAppointment (Username, AppointmentDate, AppointmentNote) Values ('" + Environment.UserName + "', '" + dt.Value + "', '" textBox.Note);";
OdbcCommand cmdInsert = new OdbcCommand(sqlInsert, Cn);
Cn.Open();
cmdDelete.ExecuteNonQuery();
//AppointmentNote = textBoxNote.Text
//Username = Environment.UserName
//AppointmentDate = any days between dateTimePicker1.Value and ateTimePicker2.Value
}
}