使用VS 2005 在SQL Server 2005 中创建自定义函数 UDF 时发生的问题

来源:互联网 发布:短信平台数据库设计 编辑:程序博客网 时间:2024/05/01 20:16

问题一:

对自定义的 Function 和 Store Procedure ,因必须使用限定词: static 静态类型,故其中的Porject 级别的方法和属性及变量,必须为 Static 类型。方法内部定义的变更则不受限定。否则会出错。

 

问题二:

如需要在Function 中读取SQL Server 中的内容,需要在其属性标识中加入DataAccess 属性。

[Microsoft.SqlServer.Server.SqlFunction(Name = "f_FunctionName", DataAccess = DataAccessKind.Read)]

 

问题三:

在 UDF 或 SP 中联接 SQL Server ,则需要使用如下方式定义联接:

SqlConnection cnn = new SqlConnection("Context Connection=true")

 

 

问题四:

无法调用已加引用的 Web Server , 出现以下错误:

 

The type or namespace name 'wsROD' could not be found (are you missing a using directive or an assembly

 

==> 解决方法:

在Web Service 名称前加上当前Project 对应的 Namespace 即可解决,如: SPS_Assembly.wsTest

 

 

问题五:

在自定义 Function 中使用了 Web Service 后, 在调试时,运行到Create Web Serivce 时出现以下错误:

 

Cannot load dynamically generated serialization assembly. In some hosting environments assembly load functionality is restricted, consider using pre-generated serializer. Please see inner exception for more information.

 

==> 解决方法:

 1, 在Project 的 Property 属性设定界面,打开 Build 面板,对属性: Generate serialization assembly 的值,选择"ON"; --- 让程序在Build 时可以生对应的的序列化使用的dll, 如 MyAssembly.XmlSerializers.dll

2,  打开 Database 面板,对属性 Permission Level 选择 "unsafe" 值, 如不使用 unsafe ,则会出现以下错误:

assembly 'MyAssembly' is storing to a static field. Storing to a static field is not allowed in external_access assemblies

3,  Build , 然后 Deploy 此Porject;

4,  此时测试时会出现错误,主要原因是由于 Web Service 对应的序列化使用的dll 未放入 SQL Server 的 Assembly 目录中;

5,  将bin/debug 目录下的 MyAssembly.XmlSerializers.dll 序列化 dll 加入 SQL Server 的 Assembly 中:

       在 Assemblies 目录中,右击,选择  New Assembly ..., 在打开的Form 中指定序列化dll 的目录,并指定 Pemission set 的安全等级为 unsafe.

      注意此中的序列化dll 一定要和MyAssembly.dll 是同一时间Build 并产生的.

6,  此时即可进行测试。

 

可能的其它SQL DB 配置问题,可试着使用以下Script 变更配置:

 

EXEC sp_configure @configname = 'Show Advanced Options', @configvalue = 1
RECONFIGURE WITH OVERRIDE
GO
EXEC sp_configure

EXEC sp_configure @configname = 'clr enabled', @configvalue = 1
RECONFIGURE WITH OVERRIDE
GO

 

reconfigure

ALTER DATABASE CBIF_PREVIEW SET TRUSTWORTHY ON

 

 

相关参考资料:

 

Dynamic Assembly Loading

http://blogs.msdn.com/sqlclr/archive/2006/10/18/dynamic-assembly-loading.aspx

 

在SQL SERVER 2005中调用Web Service

http://tech.it168.com/db/2007-09-11/200709111157063.shtml

 

原创粉丝点击