Sql CLR用户定义函数参数传递
我是一个新加入CLR的SQL/VB人员。基本上,我想创建一个SQL函数来传递一个ID参数,并从SQL表返回相应的数据Sql CLR用户定义函数参数传递,sql,vb.net,clr,Sql,Vb.net,Clr,我是一个新加入CLR的SQL/VB人员。基本上,我想创建一个SQL函数来传递一个ID参数,并从SQL表返回相应的数据 Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Data.SqlTypes Imports Microsoft.SqlServer.Server Partial Public Class UserDefinedFunctions <Microsoft.S
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server
Partial Public Class UserDefinedFunctions
<Microsoft.SqlServer.Server.SqlFunction(DataAccess:= DataAccessKind.Read)> _
Public Shared Function FalloutProfile(ByVal CRFNID As Integer) As SqlString
Using connection As New SqlConnection("context connection=true")
connection.Open()
Dim command As New SqlCommand("SELECT RATE0ORIG FROM dbo.clsgfunc WHERE CRFNID=@pnum", connection)
command.Parameters.Add("@pnum", SqlDbType.SmallInt).Value = CRFNID
Dim reader As SqlDataReader
reader = command.ExecuteReader()
SqlContext.Pipe.Send(reader)
End Using
End Function
End Class
导入系统
导入系统数据
导入System.Data.SqlClient
导入System.Data.SqlTypes
导入Microsoft.SqlServer.Server
部分公共类UserDefinedFunctions
_
公共共享函数FalloutProfile(ByVal CRFNID为整数)为SqlString
将连接用作新的SqlConnection(“context connection=true”)
connection.Open()
Dim命令作为新的SqlCommand(“从dbo.clsgfunc中选择RATE0ORIG,其中CRFNID=@pnum”,连接)
command.Parameters.Add(“@pnum”,SqlDbType.SmallInt).Value=CRFNID
作为SqlDataReader的Dim读取器
reader=command.ExecuteReader()
SqlContext.Pipe.Send(读卡器)
终端使用
端函数
末级
我似乎无法让它工作:
System.NullReferenceException:对象引用未设置为对象的实例
有什么想法吗 微软对此有一个答案,它隐藏在以下描述中: 如果管道可用,则为SqlPipe的实例;如果在管道不可用的上下文(例如,在用户定义的函数中)中调用,则为null
因此,如果您在CLR过程中尝试此操作,它应该会起作用。从CLR函数中,它不会。Microsoft对此有一个答案,它隐藏在以下描述中: 如果管道可用,则为SqlPipe的实例;如果在管道不可用的上下文(例如,在用户定义的函数中)中调用,则为null
因此,如果您在CLR过程中尝试此操作,它应该会起作用。在CLR函数中,它不会。你能给我们更多的堆栈跟踪吗?仅仅告诉我们有一个
NullReferenceException
并不能让任何不熟悉的线索得到更多信息。NullReferenceException来自部署上述CLR后的SQL 08 R2。希望代码中有明显的内容。NullReferenceException
意味着当您尝试访问代码时,代码中的内容为null。你能试着在本地调试吗?你能给我们更多的堆栈跟踪吗?仅仅告诉我们有一个NullReferenceException
并不能让任何不熟悉的线索得到更多信息。NullReferenceException来自部署上述CLR后的SQL 08 R2。希望代码中有明显的内容。NullReferenceException
意味着当您尝试访问代码时,代码中的内容为null。你能试着在本地调试吗?