2014-02-08 38 views
3

我正在使用CLR來驗證文件是否存在使用C#方法。我在sql server中創建了一個程序集和函數,並且傳遞了一個參數。Eternal File.Exist CLR C#在從SQL Server調用但在C#中工作時不工作

C#代碼:

using System; 
using System.IO; 

public class FileExist 
{ 
    [Microsoft.SqlServer.Server.SqlFunction] 
    public static bool fn_FileExistCLR(string FilePath){  

    return System.IO.File.Exists(FilePath);     
    } 
} 

我已經創建SQL Server中的組件:

CREATE ASSEMBLY FileExistCLR2 
FROM 'C:\MSSQLTips\FileExist2.dll' 
WITH PERMISSION_SET = EXTERNAL_ACCESS 

我創建功能也:

CREATE FUNCTION [dbo].[fc_ChkFlCLR2](@filename [nvarchar](4000)) 
RETURNS nvarchar(4000) WITH EXECUTE AS CALLER 
AS 
    EXTERNAL NAME [FileExistCLR2].[FileExist].[fn_FileExistCLR] 
GO 

的參數正在被由函數傳遞給CLR。

declare @filevar as nvarchar(4000) 
set @filevar = ltrim(rtrim('T:\\Project\\XML_ErrorFile20131106.txt')) 
--set @filevar = ltrim(rtrim('C:\QA_Ticket.xls')) 
SELECT [PlayNet].[dbo].[fc_ChkFlCLRChk] (@filevar) 
GO 

本地文件被發現,而不是在網絡文件夾中的文件:

結果1 - 如果文件是一個網絡文件夾,函數返回false 結果2 - 如果文件是在本地文件夾,函數返回TRUE。

我在本地sql服務器上運行這些服務器,並嘗試添加@也無濟於事。

交叉檢查。我創建了一個C#方法,在命令行中輸出結果。奇怪的是,網絡中的相同文件正在被file.exist檢測到。請幫助

using System; 
using System.IO; 

class FileExist 
{ 
    // [Microsoft.SqlServer.Server.SqlFunction] 
    static void Main() 
     { 
     string FilePath = "T:\\Project\\XML_ErrorFile20131106.txt"; 
     string retFilePath; 
      if (File.Exists(FilePath)) 
      { 
       retFilePath = "True"; 
       Console.WriteLine(retFilePath); 
        Console.ReadLine(); 
      } 
      else 
       retFilePath = "false"; 
       Console.WriteLine(retFilePath); 
        Console.ReadLine(); 
     } 
} 

回答

1

問題很可能是SQL Server進程正在運行的用戶沒有T驅動器。使用UNC驅動符號表示這類工作會更好(也更安全)(即\\thefileshare\project\xml...txt)。

+0

+1。還有SQL的用戶可能(或者說不應該)完全訪問共享... –

+0

謝謝你們。我也使用過UNC,但沒有工作。我沒有做的是在不同的共享網絡文件夾上嘗試它,這是我最終會使用的一個文件夾。 :D和它的工作! – user3286012

相關問題