无法识别的数据库格式

我想问一下,为什么当我尝试连接excel 2000/3和2010时,会出现这个exception?

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(); } } } 

你需要通过追加来告诉提供者你使用的是Excel 97-2003(xls而不是xlsx)

 Extended Properties="Excel 8.0" 

例如

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

我不知道是什么例外,但我可能知道你在说什么。 您可能正在编译为x64位,请强制它运行为32位(x86)。 我相信可以在Build Options下的Project Properties中设置设置