我需要將文件打包到TSQL腳本中的rar歸檔中。從下面的C#-script功能,所以我編譯DLL:TSQL:執行CLR存儲函數拋出System.Security.SecurityException錯誤
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction()]
static public SqlInt32 RarFiles(SqlString WORK, SqlString TARGET, SqlString SRC, SqlString SIZE)
{
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = @"C:\Program Files\WinRAR\rar.exe";
p.Arguments = String.Format
(
"a -cfg- -ep1 -idcdp -m5 -r -s -v{0} {1} {2}",
SIZE.ToString(),
TARGET.ToString(),
SRC.ToString()
);
p.WorkingDirectory = WORK.ToString();
Process x = Process.Start(p);
return x.ExitCode;
}
}
用命令:
%SYSTEMROOT%\Microsoft.NET\Framework64\v2.0.50727\csc.exe /target:library c:\test\rarfiles.cs
然後在TSQL我創建組件,代碼:
ALTER AUTHORIZATION ON DATABASE::[Test] TO [sa];
GO
ALTER DATABASE [Test] SET TRUSTWORTHY ON
GO
CREATE ASSEMBLY [CLRFunctions]
FROM 'C:\test\RarFiles.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS;
GO
CREATE FUNCTION [dbo].RarFilesCLR
(
@work [nvarchar](max),
@target [nvarchar](max),
@source [nvarchar](max),
@size [nvarchar](max)
)
RETURNS INT
AS EXTERNAL NAME CLRFunctions.UserDefinedFunctions.RarFiles;
GO
最後,我試圖執行功能:
DECLARE @work nvarchar(max) = 'c:\test';
DECLARE @target nvarchar(max) = 'c:\test\res.rar';
DECLARE @source nvarchar(max) = 'c:\test\source';
DECLARE @size nvarchar(max) = '20M';
SELECT [dbo].RarFilesCLR(@work, @target, @source, @size);
拋出一個錯誤
System.Security.SecurityException:
in UserDefinedFunctions.RarFiles(SqlString WORK, SqlString TARGET, SqlString SRC, SqlString SIZE)`...
有人能向我解釋什麼是錯在這裏?
你的(SQL服務器服務)帳戶權限的路徑應該位於WinRAR.exe,源文件和目標權限? – Deadsheep39
@ Deadsheep39我不確定。但我正在自己的筆記本電腦上測試代碼,所以我希望我所做的全部都是管理員權限... –
請更新完整的錯誤消息,因爲它會在搜索時幫助其他人。謝謝。 –