Der Versuch, reguläre csv-Dateien mit oledb zu öffnen, damit wir Abfragen für sie durchführen können (DISTINCT, UPDATE, SELECT usw.) . Scheint nicht, meine Verbindungszeichenfolge richtig zu bekommen.C# .NET 4.6.1 OleDbConnection-Fehler: Das Format der Initialisierungszeichenfolge entspricht nicht der Spezifikation ab Index 152
FYI: Der 'Logger' ist eine interne Dienstprogrammklasse, die in die VS-Debugausgabe und in eine separate .log-Datei für die Elternklasse schreibt.
conn ist eine private OleDbConnection in der Klasse
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;
}
}
}
Logger zeigt die ACCESS_CONN Variable ist:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\server\share\DEV\folder1\folder2\folder3\Extended Properties = "text; HDR=Yes; FMT=Delimited";
Dann in einem öffentlichen Verfahren:
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;
}
Der Ausgang debug hat Folgendes ist das primäre Problem:
Ich vermute, es gibt ein Syntaxproblem in der Verbindungszeichenfolge, aber ich kann nicht scheinen, eine gute Ressource für die Einrichtung in meinem speziellen Fall zu finden (oledb, delimiter = ',' kein Schema verwenden .init Datei, etc)
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
EDIT:
die connStr geändert OleDbConectionStringBuilder pro SLaks Beratung zu verwenden:
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();
Nun ist die Konsole die Verbindung zeigt offen ist, aber immer noch, sagt die connstr nicht initialisiert:
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
Verwenden Sie 'OleDbConnectionStringBuilder'. – SLaks
hinzugefügt ein anderes debug: if (""! = Conn.ConnectionString) Logger.Debug ("Conn ist offen mit" + conn.ConnectionString + ", Zustand ist:" + conn.State, Methode) ;, die es zeigt ist offen – Keith
@SLaks, ich habe Probleme, Führer online zu finden, wie man das benutzt. Ich kann leicht genug sehen ([link] (https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnectionstringbuilder (v = vs.90) .aspx), wie die ConnStr zu bauen, aber wie geht das in das conn-Objekt ein? – Keith