无法访问文件路径,因为文件夹名称包含空格和特殊字符

发布于 2024-10-07 22:05:17 字数 582 浏览 0 评论 0原文

我正在访问硬编码路径中的 DBF 数据库文件,但文件夹名称包含 ex--BSTR-VSD、BSTR~VSD 的特殊字符,我无法重命名它。

所以当我建立 odbc 连接时说 odb 然后将查询放入 odb.commandText = select * from PATH(包含具有特殊字符的文件夹名称的硬编码路径)然后给出错误

示例:

System.Data.Odbc.OdbcCommand oCmd = oConn.CreateCommand(); oCmd.CommandText = "SELECT * FROM "+ 路径名 + " 其中 DATE_Y >=110 且 DATE_M >= " + From_Month + " 且 DATE_D>=" + From_Day + " 且 DATE_Y <=110 且 DATE_M <= " + To_Month + " 和 DATE_D<=" + To_Day + " ";

dt_Dbf.Load(oCmd.ExecuteReader());

和例外:: 错误 [42000] [Microsoft][ODBC dBase 驱动程序] FROM 子句中存在语法错误。

I am accessing DBF database file which is in hardcoded path, but the folders name contains special character for ex--BSTR-VSD,BSTR~VSD and I can't rename that.

so when i am making odbc connection say odb and then putting the query into
odb.commandText = select * from PATH(hard coded path which contain folder names having special character) then it gives the error

Example:

System.Data.Odbc.OdbcCommand oCmd = oConn.CreateCommand();
oCmd.CommandText = "SELECT * FROM "+ Pathname + " where DATE_Y >=110 and DATE_M >= " + From_Month + " and DATE_D>=" + From_Day + " and DATE_Y <=110 and DATE_M <= " + To_Month + " and DATE_D<=" + To_Day + " ";

dt_Dbf.Load(oCmd.ExecuteReader());

and exception::
ERROR [42000] [Microsoft][ODBC dBase Driver] Syntax error in FROM clause.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青春如此纠结 2024-10-14 22:05:17

用括号括起来

SELECT * 
  FROM ["+ Pathname + "]
 where DATE_Y >=110 
   and DATE_M >= " + From_Month + " 
   and DATE_D>=" + From_Day + " 
   and DATE_Y <=110 
   and DATE_M <= " + To_Month + "
   and DATE_D<=" + To_Day + " "

您还可以使用诸如
开始日期和结束日期之间

Surround with Brackets

SELECT * 
  FROM ["+ Pathname + "]
 where DATE_Y >=110 
   and DATE_M >= " + From_Month + " 
   and DATE_D>=" + From_Day + " 
   and DATE_Y <=110 
   and DATE_M <= " + To_Month + "
   and DATE_D<=" + To_Day + " "

You can also clean this up using things like
betweem StartDate and EndDate

超可爱的懒熊 2024-10-14 22:05:17

为什么不使用参数化查询?您可以使用OdbcParameter 类

MSDN:它匹配字符串的整个长度,包括任何填充尾随空格。

        System.Data.Odbc.OdbcCommand oCmd = oConn.CreateCommand();
        oCmd.CommandText = "SELECT * FROM  @pathname where DATE_Y >=110 and DATE_M >= @from_Month and DATE_D>= @from_Day and DATE_Y <=110 and DATE_M <= @to_Month and DATE_D<= @to_Day";
        oCmd.Parameters.Add(new OdbcParameter("@pathname", Pathname));
        oCmd.Parameters.Add(new OdbcParameter("@from_Month", From_Month));
        oCmd.Parameters.Add(new OdbcParameter("@to_Month", To_Month));
        oCmd.Parameters.Add(new OdbcParameter("@from_Day", From_Day));
        oCmd.Parameters.Add(new OdbcParameter("@to_Day", To_Day));

Why not using Parametrized Query? You can use OdbcParameter class.

MSDN: It matches the entire length of the string, including any padding trailing spaces.

        System.Data.Odbc.OdbcCommand oCmd = oConn.CreateCommand();
        oCmd.CommandText = "SELECT * FROM  @pathname where DATE_Y >=110 and DATE_M >= @from_Month and DATE_D>= @from_Day and DATE_Y <=110 and DATE_M <= @to_Month and DATE_D<= @to_Day";
        oCmd.Parameters.Add(new OdbcParameter("@pathname", Pathname));
        oCmd.Parameters.Add(new OdbcParameter("@from_Month", From_Month));
        oCmd.Parameters.Add(new OdbcParameter("@to_Month", To_Month));
        oCmd.Parameters.Add(new OdbcParameter("@from_Day", From_Day));
        oCmd.Parameters.Add(new OdbcParameter("@to_Day", To_Day));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文