I have a string that contains 9 characters, for example LON052012 (from London 23.05.2012. - day.month.year.). I want to delete all records from table in Access where all cities start with first three letters from that string and have those months and year (05.2007.).
This is the function:
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
try
{
string sTown, s1, s2;
sTown = textBox1.Text.Substring(0, 3);
s1 = textBox1.Text.Substring(3, 2);
s2 = textBox1.Text.Substring(5);
string sDelete = "DELETE * FROM Country WHERE Town LIKE @p1 and month(TownDate) = @p2 and year(TownDate) = @p3";
OleDbCommand komDelete = new OleDbCommand(sDelete, connection);
komDelete.Parameters.AddWithValue("@p1", sTown + "*");
komDelete.Parameters.AddWithValue("@p2", s1);
komDelete.Parameters.AddWithValue("@p3", s2);
connection.Open();
komDelete.ExecuteNonQuery();
MessageBox.Show("Deleted");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
connection.Close();
}
}
else
{
MessageBox.Show("TextBox is empty!");
}
}
This is how table Country looks like (CountryID, Town, TownDate):
It always says that the record is deleted even if it's not.