简体   繁体   English

无法识别的数据库格式

[英]Unrecognized database format

I would like to ask, why I get this exception when I try to connect excel 2000/3 and also 2010? 我想问一下,当我尝试连接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();
        }
    }
}

You need to tell the provider you're using Excel 97-2003 (xls as opposed to xlsx) by appending: 你需要通过追加告诉提供商你正在使用Excel 97-2003(xls而不是xlsx):

Extended Properties="Excel 8.0"

Eg 例如

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

I dont know what the exception is but I may know what you are talking about. 我不知道异常是什么,但我可能知道你在说什么。 You are probably compiling as x64 bit, please force it to run as 32 bit (x86). 您可能正在编译为x64位,请强制它以32位(x86)运行。 I believe that setting can be set in your Project Properties under Build Options 我相信可以在Build Options下的Project Properties中设置设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM