SQL Server ->> CLR编程问题汇总

1) CLR不支持C#类方法重载(Method Overload)

今天打算写个枚举目录的C# CLR存储过程,结果发现原来CLR不支持方法重载.

Msg 6572, Level 16, State 1, Procedure GetFileList, Line 12
More than one method, property or field was found with name 'GetFileList' in class 'StoredProcedures.EnumerateSourceFileDirectory' in assembly 'XXXXXX'. Overloaded methods, properties or fields are not supported.

2) SqlString类型对应的是NVARCHAR而不是VARCHAR

CREATE PROCEDURE GetFileList
(
@SourceFolder NVARCHAR(2000),
@BeginModDate DATETIME,
@EndModDate DATETIME,
@FileExtension NVARCHAR(50),
@FileNamePattern NVARCHAR(2000),
@IsSubfolderScanned SMALLINT
)
AS EXTERNAL NAME XXXX_CLR_Lib.[StoredProcedures.EnumerateSourceFileDirectory].GetFileListByBeginAndEnd;

Msg 6552, Level 16, State 3, Procedure GetFileList, Line 12
CREATE PROCEDURE for "GetFileList" failed because T-SQL and CLR types for parameter "@SourceFolder" do not match.

3) SQL Server中使用CLR的先决条件。 BTW,如果想要操作诸如想文件系统级别的东西,比如枚举文件目录的文件或写日志记录到Windows Event Log中,在创建ASSEMBLY的时候就必须把PERMISSION设为EXTERNAL_ACCESS.

--TRUSTWORTHY is required to be turned on
ALTER DATABASE [XXXXXX] SET TRUSTWORTHY ON
GO --enable clr feature
EXEC sp_configure 'show advanced option', 1
GO RECONFIGURE
GO EXEC sp_configure 'clr enabled', 1
GO RECONFIGURE
GO /*keep user database owner the same as master database, or it will receive an error Msg 33009, Level 16, State 2, Line 2
The database owner SID recorded in the master database differs from the database owner SID recorded in database ''. You should correct this situation by resetting the owner of database '' using the ALTER AUTHORIZATION statement.
**/
DECLARE @Command VARCHAR(MAX) = 'ALTER AUTHORIZATION ON DATABASE::<<DatabaseName>> TO
[<<LoginName>>]' SELECT @Command = REPLACE(REPLACE(@Command
, '<<DatabaseName>>', SD.Name)
, '<<LoginName>>', SL.Name)
FROM master..sysdatabases SD
JOIN master..syslogins SL ON SD.SID = SL.SID
WHERE SD.Name = DB_NAME() --PRINT @Command
EXEC(@Command)
上一篇:android Titlebar一行代码实现沉浸式效果


下一篇:在SQL Server中创建用户角色及授权(使用SQL语句)