Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从c执行sql查询时更改文件路径#_C#_Sql_Sql Server 2005 - Fatal编程技术网

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

C# 从c执行sql查询时更改文件路径#,c#,sql,sql-server-2005,C#,Sql,Sql Server 2005,我有一个sql数据库,其中包含一些文件的字符串文件路径和文件名。我使用VisualStudio2008作为IDE和SQLServer2005进行开发 如果我试图执行SQL查询以获取文件路径,它将返回正确的结果。但当我在c#中从windows应用程序执行SQL查询时,它返回的文件路径中的所有\都更改为/ 以下是我从SQL Server Management Studio执行的SQL查询: select FilePath FROM dbo.[tbl_name] WHERE SerialNo = 2;

我有一个sql数据库,其中包含一些文件的字符串文件路径和文件名。我使用VisualStudio2008作为IDE和SQLServer2005进行开发

如果我试图执行SQL查询以获取文件路径,它将返回正确的结果。但当我在c#中从windows应用程序执行SQL查询时,它返回的文件路径中的所有
\
都更改为
/

以下是我从SQL Server Management Studio执行的SQL查询:

select FilePath FROM dbo.[tbl_name] WHERE SerialNo = 2;
它导致
FilePath
C:\ProgramFiles\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 ();
}
但当我尝试使用下面提到的C#windows窗体代码时。我得到了一个错误的
FilePath
C://ProgramFiles//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 ();
}

可能是什么问题?

我猜您在调试器中看到了此路径调试器只是转义路径,没有其他内容。如果你把它写在某个地方,比如控制台,你会得到原来的路径(带有单反斜杠)。

为什么你认为它是一个问题?因为它正在传递错误的文件路径来处理.Stand()方法,并且抛出一个异常作为系统找不到指定的文件。然后我试着调试,发现了这个问题。代码的process.start(FilePath)行中出现异常,这意味着它传递给process.start()方法的FilePath不正确。建议任何其他解决方案。斜杠应为\n而不是/。我以为是你的打字错误。是的,是打字错误。我获得的文件路径为C:\\Program Files\\Test\\Mydoc.pdf。但我正在使用控制台获得正确的路径。不知道如何解决它!。您得到的异常是什么?System.ComponentModel.Win32Exception:系统找不到指定的文件。