Unrecognized database format

I would like to ask why I get this exception when I try to connect excel 2000/3 as well as 2010?

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;

namespace md1_connect
{
    class Program
    {

        static void Main (string[] args)
        { 
            string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"Book1.xls\"";            
            OleDbConnection MyConn = new OleDbConnection(ConnectionString);
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM[Sheet2$]", MyConn);
            MyConn.Open();
            OleDbDataReader dataReader = cmd.ExecuteReader();

            while (dataReader.Read())
            {
                Console.WriteLine(dataReader.GetDouble(0));
            }
            MyConn.Close();
        }
    }
}
+3
source share
2 answers

You need to tell the provider that you are using Excel 97-2003 (xls unlike xlsx), adding:

Extended Properties="Excel 8.0"

eg.

string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"Book1.xls\";Extended Properties=\"Excel 8.0\"";
+1
source

I do not know what the exception is, but I know what you are talking about. Most likely you are compiling as x64 bits, make it work as 32 bits (x86). I believe that the setting can be set in the Project Properties under Build Settings

+1
source

All Articles