Hi VAer-4038,
Based on your code, you need not put delete statement in a for loop.
You can just use the delete sql to delete records that meet the conditions.
Then you can use a for loop to insert the records which date from dateTimePicker1 to dateTimePicker2.
Note that you need to use betwee operator to select values within a given range in your "sqlDelete".
Here is a test code example you can refer to.
private void button1_Click(object sender, EventArgs e)
{
using (OdbcConnection Cn = new OdbcConnection(connectionString)) //Access database
{
string sqlDelete = "DELETE FROM Table1 WHERE Name = '" + textBox1.Text + "' And Field2 BETWEEN '" + dateTimePicker1.Value.Date + "' AND '" + dateTimePicker2.Value.Date + "'";
OdbcCommand cmdDelete = new OdbcCommand(sqlDelete, Cn);
Cn.Open();
int i = cmdDelete.ExecuteNonQuery();
//You can also check if the delete statement is executed successfully with the "i"
//if (i>0)
//{
// MessageBox.Show("delete successfully");
//}
for (DateTime dt = dateTimePicker1.Value.Date; dt <= dateTimePicker2.Value.Date; dt = dt.AddDays(1))
{
string sqpInsert = "INSERT INTO Table1(Name, Field2, Field1) Values ('" + textBox2.Text + "', '" + dt.Date + "', '" + textBox3.Text + "')";
OdbcCommand cmdInsert = new OdbcCommand(sqpInsert, Cn);
cmdInsert.ExecuteNonQuery();
}
}
}
Best Regards,
Daniel Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.