English 中文(简体)
Data type mismatch in criteria expression From Excel file through OLEDB
原标题:
  • 时间:2009-11-17 23:07:35
  •  标签:
  • excel
  • oledb

I am processing an uploaded file through a ASP.NET page. I am using the following connection string:

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
     excelFile + @";Extended Properties=""Excel 8.0;HDR=YES;""";

Here is the SQL statement:

string sql = "SELECT * FROM [Sheet1$] WHERE [req_tf_order_no] <>   ";

Here is the code that loops through the DataReader

   using (OleDbConnection connection = new OleDbConnection(connectionString))
   {
       using (OleDbCommand command = connection.CreateCommand())
       {
           command.CommandText = sql;
           connection.Open();
           using (OleDbDataReader reader = command.ExecuteReader())
           {
               if (reader.HasRows)
               {
                   hasMoreData = reader.Read();
                    while(hasMoreData)
                   {
                            ...
                            hasMoreData = reader.Read();
                           if (hasMoreData == false)
                           {
                               break;
                           }
                       }
                   }
               }

Here is most of the stack trace:

(Error Description: Data type mismatch in criteria expression.)
(Stack Trace: at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.OleDb.OleDbCommand.ExecuteReader() at ...

If I don t include the WHERE clause(WHERE [req_tf_order_no] <> ), I don t get any error. The only reason for the WHERE clause is to filter out cleared but not deleted rows in the input file.

I suspect there is something in the formatting or data of the input Excel file that is causing this. I have had some input files that have not thrown an exception but most do. What is causing this error and how can it be fixed? Is there a better way to accomplish what I am trying to do? Can I perhaps have the user upload a different file format?

Edit Found that checking for null rather than an empty string in the SQL statement works without an exception being thrown.

string sql = "SELECT * FROM [Sheet1$] WHERE [req_tf_order_no] IS NOT NULL";

问题回答

Found that checking for null rather than an empty string in the SQL statement works without an exception being thrown.

string sql = "SELECT * FROM [Sheet1$] WHERE [req_tf_order_no] IS NOT NULL";





相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签