0

\I have three columns in an access database table (DATA) as shown below

enter image description here

I just want to delete some rows based of two conditions in the WHERE clause in the SQL query ; for ex, delete row when NAME = "A" and Date = "1/1/2017"

I used DELETE from DATA Where Name='A' and Date='1/1/2017' This gives "type mismatch error"!

Here is the code in C#:

 using (OleDbConnection thisConnection = new OleDbConnection(connectionname))
 {
    string deletequery = " DELETE FROM DATA WHERE [Name] = 'A' And [Date] = '1/1/2017';

    OleDbCommand myAccessCommandDelete = new OleDbCommand(deletequery, thisConnection);
    thisConnection.Open();
    myAccessCommandDelete.ExecuteNonQuery();
    thisConnection.Close();
}
6
  • this isnt a c# issue! it doesnt like your format of date. please remove the c# tag Commented Feb 20, 2017 at 10:51
  • I added the C# code. I was first trying it in Access and would have used the same query in C#. @BugFinder Commented Feb 20, 2017 at 10:54
  • @Steve : Posted it Commented Feb 20, 2017 at 10:55
  • That's not the correct way to pass values if the field (DATA) is a field of type DateTime. Is Data a DateTime field in access? Commented Feb 20, 2017 at 10:57
  • yes it is defined as "datetime" @Steve Commented Feb 20, 2017 at 10:58

1 Answer 1

4

The best way to pass values to a database engine that will be used in a query is through the parameters collection specifying exactly the type of the parameter

using (OleDbConnection thisConnection = new OleDbConnection(connectionname))
{
    string deletequery = @"DELETE FROM DATA WHERE [Name] = @name And 
                         [Date] = @date";
    OleDbCommand myAccessCommandDelete = new OleDbCommand(deletequery, thisConnection);
    thisConnection.Open();
    myAccessCommandDelete.Parameters.Add("@name", OleDbType.VarWChar).Value = "A";
    myAccessCommandDelete.Parameters.Add("@date", OleDbType.Date).Value = new DateTime(2017,1,1);
    myAccessCommandDelete.ExecuteNonQuery();
    // not needed -> thisConnection.Close();
}

In this way you don't leave space to interpretation (conversion from string to date) of your values but you tell exactly to your db engine what your value is. And of course if you specify the correct type you can't have a Type Mismatch error

Sign up to request clarification or add additional context in comments.

2 Comments

aaah interesting! I suspected the issue was because I used "Date" as the column name; Date being a keyword. Thanks! @Steve
Using parameters is the way to go for every thing you need to pass as value for your columns (WHERE, INSERT,UPDATE,DELETE) Also, albeit more difficult to exploit in MS-Access 'thanks' to the missing batch update functionality, it is the correct way to avoid Sql Injection mess

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.