无法识别的数据库格式

时间:2014-02-12 16:41:30

标签: c# database excel

我想问一下,为什么当我尝试连接excel 2000/3和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();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您需要通过附加来告诉提供商您正在使用Excel 97-2003(xls而不是xlsx):

Extended Properties="Excel 8.0"

E.g。

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

答案 1 :(得分:1)

我不知道异常是什么,但我可能知道你在说什么。您可能正在编译为x64位,请强制它以32位(x86)运行。我相信可以在构建选项

下的项目属性中设置设置
相关问题