将SQL函数ReturnValue引入前端表单ListBox

时间:2014-03-28 19:10:58

标签: sql-server vb.net sql-function

我有一个带有按钮和列表框的表单。代码按钮如下所示

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim dc As New DataTestDataContext
    Dim x As List(Of SplitResult)

    x = dc.Split("1, 2, 3, 4", ",")
    ListBox1.Items.Add(x)

  End Sub

我正在使用一个具有我的功能的" Split"在其中。我的split函数的代码如下所示

 <FunctionAttribute(Name:="dbo.Split", IsComposable:=true)>  _
 Public Function Split(<Parameter(Name:="String", DbType:="VarChar(MAX)")> ByVal [string] As String, <Parameter(Name:="Delimiter", DbType:="Char(1)")> ByVal delimiter As System.Nullable(Of Char)) As IQueryable(Of SplitResult)
    Return Me.CreateMethodCallQuery(Of SplitResult)(Me, CType(MethodInfo.GetCurrentMethod, MethodInfo), [string], delimiter)
End Function

该函数有两个参数。字符串和分隔符。我选择的分隔符是逗号

我的SQL函数的代码如下所示

 ALTER FUNCTION [dbo].[Split](@String varchar(MAX), @Delimiter char(1))       
 returns @temptable TABLE (items varchar(MAX))       
 as       
 begin      
declare @idx int       
declare @slice varchar(8000)       

select @idx = 1       
    if len(@String)<1 or @String is null  return       

while @idx!= 0       
begin       
    set @idx = charindex(@Delimiter,@String)       
    if @idx!=0       
        set @slice = left(@String,@idx - 1)       
    else       
        set @slice = @String       

    if(len(@slice)>0)  
        insert into @temptable(Items) values(@slice)       

    set @String = right(@String,len(@String) - @idx)       
    if len(@String) = 0 break       
end  

 return  

我已经测试了SQL函数并且它正常工作,我只想知道如何从函数返回值到我的列表框。当我单独运行该功能时,结果为表格式,如下所示

 items
 |1|
 |2|
 |3|
 |4|

提前致谢

0 个答案:

没有答案