【发布时间】:2012-10-16 15:39:30
【问题描述】:
当目录名中没有空格时使用以下方法
对于字符串file1,它将使用字符串file2执行reader,它会创建一个异常吗?对于我们的一些客户,我需要访问路径中有空格的文件夹。
file1 = "C:\\test1\\file.dbf";
file2 = "C:\\test 2\\file.dbf";
OdbcConnection Connection = new OdbcConnection();
Connection.ConnectionString = @"Driver={Microsoft Visual FoxPro Driver};Exclusive=No;SourceType=DBF;SourceDB=" + strFilename + ";";
Connection.Open();
OdbcCommand Command = Connection.CreateCommand();
Command.CommandText = @"SELECT * FROM " + file1; //Command.CommandText = @"SELECT * FROM " + file2;
一旦执行 连接.打开(); 它正确打开它,
一旦我们执行 OdbcDataReader Reader = Command.ExecuteReader();
我得到 {System.Data.Odbc.OdbcException: ERROR [42S02] [Microsoft][ODBC Visual FoxPro Driver]File 'file2.dbf' does not exist.
回答,因为我没有足够的代表
感谢大家对我的问题的解决方案的贡献。该解决方案要求我放置完整路径(其中有一个空格)并使用 @ char 使其成为文字字符串。出于某种原因,通过在引号中加上转义字符(“\””)使其文字化并不能解决问题。
综上所述,我在连接字符串和命令字符串中的路径前面都使用了@。
strFilename = S.ImportFolder + "\\" +"file"+ ".dbf";
OdbcConnection Connection = new OdbcConnection();
Connection.ConnectionString = @"Driver={Microsoft Visual FoxPro Driver};Exclusive=No;SourceType=DBF;SourceDB="+@strFilename+";";
Connection.Open();
OdbcCommand Command = Connection.CreateCommand();
Command.CommandText = @"SELECT * FROM "+ @strFilename;
OdbcDataReader Reader = Command.ExecuteReader();
【问题讨论】:
-
可以公平地说,它没有连接到数据库,虽然很高兴知道实际的错误消息是什么。
标签: c#