问题
I have the following c# code in a WPF app:
var command = new OleDbCommand($"CREATE INDEX idx{index.ColumnName} ON {tableConfig.Name}({index.ColumnName})", _targetCon);
try
{
command.ExecuteNonQuery();
}
catch (Exception ex1) { _serilog.WriteError(METHOD_NAME, "Trouble adding index onto Access column", ex1); }
My problem is that when stepping through the code in the VS debugger, the line "command.ExecuteNonQuery();" is throwing an exception. At the time of the exception, the value of _targetCon.ConnectionString is "Provider=Microsoft.Jet.OleDb.4.0;Data Source=C:\AccessDatabases\APMS\unit3.mdb;"
and the value of the _targetCon.DataSource is: C:\AccessDatabases\APMS\unit3.mdb
This is the correct path. However, the value of ex2.Message is "Could not find file 'C:\Git_RB\AccessMigration\AccessLauncher\AccessLauncher.WPF\bin\Debug\Unit3.mdb'"
If I run the app outside of VS then I find that it's still trying to find the file in the database in the same folder as the exe:
Why is the command object ignoring the properties of its connection object and looking in the wrong path?
回答1:
Check the actual value of the SQL statement that you are using. It might contain another table name than the one you expect, e.g. due to string truncation.
来源:https://stackoverflow.com/questions/51281347/oledbcommand-using-wrong-path