2
votes

My connection string

    <add connectionString="Driver={Microsoft Text Driver (*.txt; *.csv)}; Dbq=E:; Extensions=asc,csv,tab,txt;" 
     name="TestConnectionString"/>

My C# code:

        protected void ExecuteButton_Click(object sender, EventArgs e)
    {
        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
        using (OdbcConnection con = new OdbcConnection(connectionString))
        {
            con.Open();
            string sql = "SELECT date_id FROM date_conversion.csv WHERE [date_in_ad] = '4/13/1944'";
            using (OdbcCommand command =  new OdbcCommand(sql, con))
            {
                ResultTextBox.Text = Convert.ToString(command.ExecuteScalar());
            }
        }
    }

My CSV file:

date_id date_in_ad ad_month_id ad_date_id ad_year_id
  1      4/13/1944    4           13          1944
  2      4/14/1944    4           14          1944
  3      4/15/1944    4           15          1944
  4      4/16/1944    4           16          1944
  5      4/17/1944    4           17          1944

I am receiving following error: System.Data.Odbc.OdbcException: ERROR [22018] [Microsoft][ODBC Text Driver] Data type mismatch in criteria expression.

2

2 Answers

1
votes

So, lets rename the question to "I get a sql exception, please help"

It has nothing to do with csv. And from your code only one line is relevant:

string sql = "SELECT date_id FROM date_conversion.csv WHERE [date_in_ad] = '4/13/1944'";

Let's have a look again at the error:

Data type mismatch in criteria expression

THere is only one criteria - [date_in_ad] and a date literal.

http://msdn.microsoft.com/en-us/library/ms710282(v=vs.85).aspx

has nice exampls how dates should look for ODBC....

http://technet.microsoft.com/en-us/library/ms190234(v=sql.90).aspx

has more explanations. And no, they do absolutely not look like your string.

Date constants look like:

{ d '1990-10-02' }

To make things easy - use a parameter. Please.

0
votes

Check MS KB

Try this query

string sql = "SELECT date_id FROM date_conversion.csv WHERE [date_in_ad] = #4/13/1944#";