excel columns selection query in C# winform -


I am trying to match a date with a column in excel sheet whose column type is datetime, using the following query Do

  dateTimeDTNV = convert toDateTime ("7/16/2010"); OleDbDataAdapter da = New OleDbDataAdapter ("Select * FROM [" + SheetName + "$] where [date of hand] = '" + DTN + "' ', Con);  

Testimonial expression in datatype mismatch.

Please help.

Since you are using JET DB provider (I'm assuming here), which means your connection string looks like this:

  string connection string = @ "provider = Microsoft.Jet.OLEDB.4.0; Data source = c: \ temp \ SOTest3.xls; Extended Properties = "Excel 8.0; HDR = Yes"; "";  

Your dates must be surrounded by '#' signals (excluding single quotes). it should work.

Here's an example that works for me:

  Fixed zero main (string [] args) {string connectionString = @ "Provider = Microsoft.Jet OLEDB. 4.0; Data Source = C: \ Temp \ SOTest3.xls; Extended Properties = "Excel 8.0; HDR = Yes; "" ;; DbProviderFactory factory = DbProviderFactories.GetFactory ("System.Data.OleDb"); (Using DBC Connection Connection = Factory.Contact Connection ()) (use connection.ConnectionString = connectionString; (DbCommand command = connection.CreateCommand ()) (connection.Open (); // Open connection DateTime DTNEV = Convert.Todetime ("7/21/2010"); DbDataAdapter da = factory.CreateDataAdapter (); Da.SelectCommand = Command; Da.SelectCommand.Connection = connection; Da.SelectCommand.CommandText = "SELECT * FROM [Sheet1 $ ] WHERE [HAND OF DEATH] = # "+ dtNew.ToString (" yyyy-MM-dd ") +" # "; Datatelle dtDate = New Datatale (); da.Fill (dtDate); Console.WriteLine (dtDate.Rows .ount); Console.ReadLine ();}}}  

Hope it helps


Comments