调用OleDbConnection open方法时OleDbException

时间:2013-01-09 16:12:10

标签: asp.net .net asp.net-mvc ado.net asp.net-mvc-4

我正在尝试在我的asp net mvc应用程序中操作excel 2010文件。

这是代码:

string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\teste.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";
OleDbConnection conexao = new OleDbConnection(connectionString);
conexao.Open();

Open()方法拖出一个我无法找到原因的异常。

例外:

    Erro: System.Data.OleDb.OleDbException (0x80004005): Failure creating file.
   at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
   at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.OleDb.OleDbConnection.Open()
   at Gedi.Controllers.AdminController.File() in c:\Users\Guilherme\Documents\Visual Studio 2012\Projects\Gedi\Gedi\Controllers\AdminController.cs:line 62

1 个答案:

答案 0 :(得分:2)

使用带有逐字字符串的文件路径中的变量:

string path = @"c:\teste.xlsx";
string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");", path);

否则你需要掩盖像[{1}}:

这样的反斜杠