简体   繁体   English

从C#执行sql查询时更改文件路径

[英]Filepath changing while executing sql query from c#

I have a sql database where i am string filepath and file name of some files. 我有一个sql数据库,其中我是字符串filepath和某些文件的文件名。 I am using Visual Studio 2008 as IDE and SQL Server 2005 for development. 我使用Visual Studio 2008作为IDE和SQL Server 2005进行开发。

If I am trying to execute a SQL query to get filepath it returns correct result. 如果我试图执行SQL查询以获取文件路径,它将返回正确的结果。 But when I am executing SQL query from windows application in c#, it is returning Filepath in which all \\ are changed to // . 但是,当我在C#中从Windows应用程序执行SQL查询时,它返回的Filepath中所有\\都更改为//

Here is the SQL query I executed from SQL Server Management Studio: 这是我从SQL Server Management Studio执行的SQL查询:

select FilePath FROM dbo.[tbl_name] WHERE SerialNo = 2;

It results in FilePath being C:\\Program Files\\Test\\Mydoc.pdf 结果导致FilePathC:\\Program Files\\Test\\Mydoc.pdf

But when I am trying through C# windows forms code as mentioned below. 但是当我尝试通过C#Windows窗体代码时,如下所述。 I am getting a wrong value for FilePath : C://Program Files//Test//Mydoc.pdf 我的FilePath值错误: C://Program Files//Test//Mydoc.pdf

try
{
     using (connection = new SqlConnection(connectionString))
     {
          connection.Open();
          using (SqlCommand command = new SqlCommand("SELECT FilePath FROM dbo.[tbl_name] WHERE SerialNo LIKE @Sno", connection))
          {
              command.Parameters.Add(new SqlParameter("Sno", Serial));
              FiletoOpen = command.ExecuteScalar().ToString();
              Process.Start(FiletoOpen );
          }
      }
}
catch (Exception ex)
{
     MessageBox.Show(ex.ToString(), "Exception has occured!", MessageBoxButtons.OK);
}
finally
{
      connection.Close ();
}

What may be the problem? 可能是什么问题?

I guess you are seeing this path in your debugger. 我猜您在调试器中看到了这个路径。 Debugger is just escaping the path and nothing else . 调试器只是转义路径而已 If you write it somewhere, like console, you will get the original path (with single backslashes). 如果将其写在某个位置(例如控制台),则将获得原始路径(带有单个反斜杠)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM