c#.NET 4.6.1 OleDbConnection error: Format of the initialization string does not conform to specification starting at index 152

c#.NET 4.6.1 OleDbConnection error: Format of the initialization string does not conform to specification starting at index 152

尝试使用 oledb 打开常规 csv 文件,以便我们可以对它们执行查询(DISTINCT、UPDATE、SELECT 等)。似乎无法正确设置我的连接字符串。

仅供参考:'Logger' 是一个内部实用程序 class,它写入 VS 调试输出和父 class 的单独 .log 文件。

conn 是 class

处的私有 OleDbConnection
OleDbConnection connectToFile()
{
    string method = this.ToString() + ".connectToFile";
    Logger.Debug("starting", method);
    Logger.Debug("strFolderPath = " + strFolderPath, method);
    string ACCESS_CONN = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFolderPath.Substring(0,strFolderPath.Length) + ";Extended Properties = \"text; HDR=Yes; FMT=Delimited(,)\";");
    Logger.Debug("ACCESS_CONN = " + ACCESS_CONN, method);
    using (conn = new OleDbConnection())
    {
        try
        {
            conn.ConnectionString = ACCESS_CONN;
            Logger.Debug("connection string: " + conn.ConnectionString, method);
            conn.Open();
            if ("" != conn.ConnectionString)
                Logger.Debug("Conn is open", method);
            else
                Logger.Debug("Conn didn't open", method);
            return conn;
        }
        catch (Exception Ex)
        {
            Logger.Error(Ex, method);
            return null;
        }
    }

}

记录器显示 ACCESS_CONN 变量是:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\server\share\DEV\folder1\folder2\folder3\Extended Properties = "text; HDR=Yes; FMT=Delimited";

然后在public方法中:

try
{
    conn = connectToFile();  //this is the call to the method above
    Logger.Debug("Connected to File", method);
    adapter = new OleDbDataAdapter(commandString, conn);
    dt = new DataTable(strShortFileName);
    if (dt != null)
        Logger.Debug("dataAdapter in place", method);
    else
        Logger.Debug("dataAdapter IS NOT in place", method);
    adapter.Fill(dt);
}
catch (Exception Ex)
{
    Logger.Error("Connection failed to open, exception:  " + Ex.ToString(), method);
    return null;
}

调试输出如下,主要问题是:

System.InvalidOperationException: The ConnectionString property has not been initialized.

我猜连接字符串中存在语法问题,但在我的特定情况下我似乎找不到设置它的好资源(oledb,delimiter=',' not using a schema.init 文件等)

2016-05-24 13:09:46,INFO,FileParser:  adaptive.common.AdaptiveFileParser,Starting
2016-05-24 13:09:46,DEBUG,adaptive.common.AdaptiveFileParser.connectToFile,starting
2016-05-24 13:09:46,DEBUG,adaptive.common.AdaptiveFileParser.connectToFile,strFolderPath = \server\share\DEV\folder1\folder2\folder3\
2016-05-24 13:09:46,DEBUG,adaptive.common.AdaptiveFileParser.connectToFile,ACCESS_CONN = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\server\share\DEV\folder1\folder2\folder3\;Extended Properties = "text; HDR=Yes; FMT=Delimited(,)";
2016-05-24 13:09:46,DEBUG,adaptive.common.AdaptiveFileParser.connectToFile,connection string: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\server\share\DEV\folder1\folder2\folder3\;Extended Properties = "text; HDR=Yes; FMT=Delimited(,)";
'CommonTestExe.vshost.exe' (CLR v4.0.30319: CommonTestExe.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Transactions\v4.0_4.0.0.0__b77a5c561934e089\System.Transactions.dll'. Symbols loaded.
'CommonTestExe.vshost.exe' (CLR v4.0.30319: CommonTestExe.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll'. Symbols loaded.
'CommonTestExe.vshost.exe' (CLR v4.0.30319: CommonTestExe.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.Wrapper.dll'. Symbols loaded.
2016-05-24 13:09:46,DEBUG,adaptive.common.AdaptiveFileParser.connectToFile,Conn is open
2016-05-24 13:09:46,DEBUG,FileParser:  adaptive.common.AdaptiveFileParser,Connected to File
2016-05-24 13:09:46,DEBUG,FileParser:  adaptive.common.AdaptiveFileParser,dataAdapter in place
Exception thrown: 'System.InvalidOperationException' in System.Data.dll
Exception thrown: 'System.InvalidOperationException' in System.Data.dll
2016-05-24 13:09:46,ERROR,FileParser:  adaptive.common.AdaptiveFileParser,Connection failed to open, exception:  System.InvalidOperationException: The ConnectionString property has not been initialized.
   at System.Data.OleDb.OleDbConnection.PermissionDemand()
   at System.Data.OleDb.OleDbConnectionFactory.PermissionDemand(DbConnection outerConnection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   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 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
   at adaptive.common.AdaptiveFileParser.getQueryResults(String commandString) in C:\Users\kmckinley\Source\Repos\labautocommoncode\adaptive.common\adaptive.common\fileParser.cs:line 89
adaptive.common.adCommonLib, ending

编辑:

根据 SLaks 建议将 connStr 更改为使用 OleDbConectionStringBuilder:

OleDbConnectionStringBuilder connStrBuilder = new OleDbConnectionStringBuilder();
connStrBuilder.Add("Provider", "Microsoft.Jet.OLEDB.4.0");
connStrBuilder.DataSource = strFolderPath.Substring(0, strFolderPath.Length);
connStrBuilder.Add("Extended Properties", @"text; HDR=Yes; FMT=Delimited(,)");
conn.ConnectionString = connStrBuilder.ToString();
Logger.Debug("connection string: " + conn.ConnectionString, method);
conn.Open();

现在控制台显示连接已打开,但仍显示 connstr 未初始化:

2016-05-24 13:46:32,DEBUG,adaptive.common.AdaptiveFileParser.connectToFile,connection string: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\server\share\DEV\folder1\folder2\folder3\;Extended Properties="text; HDR=Yes; FMT=Delimited(,)"  'CommonTestExe.vshost.exe' (CLR v4.0.30319: 
'CommonTestExe.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll'. Symbols loaded. 
'CommonTestExe.vshost.exe' (CLR v4.0.30319: CommonTestExe.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.Wrapper.dll'. Symbols loaded.  2016-05-24 13:46:32,DEBUG,adaptive.common.AdaptiveFileParser.connectToFile,Conn is open with Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\server\share\DEV\folder1\folder2\folder3\;Extended Properties="text; HDR=Yes; FMT=Delimited(,)", state is: Open  
2016-05-24 13:46:32,DEBUG,FileParser:  adaptive.common.AdaptiveFileParser,Connected to File 
2016-05-24 13:46:32,DEBUG,FileParser:  adaptive.common.AdaptiveFileParser,dataAdapter in place  Exception thrown: 'System.InvalidOperationException' in System.Data.dll Exception thrown: 'System.InvalidOperationException' in System.Data.dll  
2016-05-24 13:46:33,ERROR,FileParser:  adaptive.common.AdaptiveFileParser,Connection failed to open, exception:  System.InvalidOperationException: The ConnectionString property has not been initialized.
    at System.Data.OleDb.OleDbConnection.PermissionDemand()
    at System.Data.OleDb.OleDbConnectionFactory.PermissionDemand(DbConnection outerConnection)
    at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
    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 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
    at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)    at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
    at adaptive.common.AdaptiveFileParser.getQueryResults(String commandString) in C:\Users\kmckinley\Source\Repos\labautocommoncode\adaptive.common\adaptive.common\fileParser.cs:line 89

看来我对变量进行了两次范围限定 'conn':

错误代码:

OleDbConnection connectToFile()
{
    string method = this.ToString() + ".connectToFile";
    Logger.Debug("starting", method);
    Logger.Debug("strFolderPath = " + strFolderPath, method);
    string ACCESS_CONN = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFolderPath.Substring(0,strFolderPath.Length) + ";Extended Properties = \"text; HDR=Yes; FMT=Delimited(,)\";");
    Logger.Debug("ACCESS_CONN = " + ACCESS_CONN, method);
    using (conn = new OleDbConnection())
    {
        try
        {
            conn.ConnectionString = ACCESS_CONN;
            Logger.Debug("connection string: " + conn.ConnectionString, method);
            conn.Open();
            if ("" != conn.ConnectionString)
                Logger.Debug("Conn is open", method);
            else
                Logger.Debug("Conn didn't open", method);
            return conn;
        }
        catch (Exception Ex)
        {
            Logger.Error(Ex, method);
            return null;
        }
    }

}

更正代码:

conn = new OleDbConnection();
try
{
    OleDbConnectionStringBuilder connStrBuilder = new OleDbConnectionStringBuilder();
    connStrBuilder.Provider = "Microsoft.Jet.OLEDB.4.0";
    connStrBuilder.DataSource = strFolderPath;
    connStrBuilder.Add("Extended Properties", @"text; HDR=Yes; FMT=Delimited(" + delim + ")");
    conn.ConnectionString = connStrBuilder.ToString();
    Logger.Debug("connection string: " + conn.ConnectionString, method);
    conn.Open();
    if ("" != conn.ConnectionString)
        Logger.Debug("Conn is open with " + conn.ConnectionString + ", state is: " + conn.State, method);
    else
    Logger.Debug("Conn didn't open", method);
    return conn;
}
catch (Exception Ex)
{
    Logger.Error(Ex, method);
    return null;
}