Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unrecognized database format

Tags:

c#

database

excel

I would like to ask, why I get this exception when I try to connect excel 2000/3 and also 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();
        }
    }
}
like image 240
Imants Volkovs Avatar asked Jan 23 '26 13:01

Imants Volkovs


1 Answers

You need to tell the provider you're using Excel 97-2003 (xls as opposed to xlsx) by appending:

Extended Properties="Excel 8.0"

E.g.

string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"Book1.xls\";Extended Properties=\"Excel 8.0\"";
like image 190
Alex K. Avatar answered Jan 26 '26 02:01

Alex K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!