使用表值参数时如何将多个参数一起传递给存储过程

时间:2013-11-04 07:24:56

标签: sql vb.net

我有一个像这样的表值参数

CREATE TYPE dbo.Loc AS TABLE(Lo integer);

我的存储过程如下所示:

ALTER PROCEDURE [dbo].[T_TransactionSummary]  
                        @startDate datetime,  
                        @endDate datetime,
                        @locations dbo.Loc readonly
              ..........................
              ...........................
WHERE     (Transaction_tbl.dtime BETWEEN @fromDate AND @toDate) 
AND (Location_tbl.Locid IN (select Lo from @locations))

我有一个包含多个项目的列表框。我可以从列表框中选择多个项目。如何将多个Locationid传递给我的存储过程

 cnt = LSTlocations.SelectedItems.Count
 If cnt > 0 Then
          For i = 0 To cnt - 1
        Dim locationanme As String = LSTlocations.SelectedItems(i).ToString
        locid = RecordID("Locid", "Location_tbl", "LocName", locationanme)
next
end if  


  Dim da As New SqlDataAdapter
            Dim ds As New DataSet
            Dim cmd23 As New SqlCommand("IBS_TransactionSummary", con.connect)
            cmd23.CommandType = CommandType.StoredProcedure
            cmd23.Parameters.Add("@startDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = startdate
            cmd23.Parameters.Add("@endDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = enddate
Dim tvp1 As SqlParameter =cmd23.Parameters.Add("@location", SqlDbType.Int).Value = locid
 tvp1.SqlDbType = SqlDbType.Structured
        tvp1.TypeName = "dbo.Loc"
     da.SelectCommand = cmd23
    da.Fill(ds)

但我收到错误..我正在使用vb.net中的Windows窗体

3 个答案:

答案 0 :(得分:1)

有一些如何在http://msdn.microsoft.com/en-us/library/bb675163%28v=vs.110%29.aspx执行此操作的示例(请参阅标题为“将表值参数传递给存储过程”一节 “)。

最简单的事情似乎是使用用户选择的值填充DataTable并将其传递给@locations参数的存储过程。

也许有些事情(注意我没有安装VB.NET,所以请把它作为它应该如何工作的大纲,不一定是可以直接使用的代码):

cnt = LSTlocations.SelectedItems.Count
' *** Set up the DataTable here: *** '
Dim locTable As New DataTable
locTable.Columns.Add("Lo", GetType(Integer))

If cnt > 0 Then
    For i = 0 To cnt - 1
        Dim locationanme As String = LSTlocations.SelectedItems(i).ToString
        locid = RecordID("Locid", "Location_tbl", "LocName", locationanme)
        ' *** Add the ID to the table here: *** '
        locTable.Rows.Add(locid)
    next
end if  

Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim cmd23 As New SqlCommand("IBS_TransactionSummary", con.connect)
cmd23.CommandType = CommandType.StoredProcedure
cmd23.Parameters.Add("@startDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = startdate
cmd23.Parameters.Add("@endDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = enddate
' *** Supply the DataTable as a parameter to the procedure here: *** '
Dim tvp1 As SqlParameter =cmd23.Parameters.AddWithValue("@location", locTable)
tvp1.SqlDbType = SqlDbType.Structured
tvp1.TypeName = "dbo.Loc"
da.SelectCommand = cmd23
da.Fill(ds)

答案 1 :(得分:0)

Table types SQL SERVER 2008

如果您的sql server版本> = 2008年。

答案 2 :(得分:0)

cnt = LSTlocations.SelectedItems.Count
' *** Set up the DataTable here: *** '
Dim locTable As New DataTable
locTable.Columns.Add("Lo", GetType(Integer))

If cnt > 0 Then
    For i = 0 To cnt - 1
        Dim locationanme As String = LSTlocations.SelectedItems(i).ToString
        locid = RecordID("Locid", "Location_tbl", "LocName", locationanme)
        ' *** Add the ID to the table here: *** '
        locTable.Rows.Add(locid)
    next
end if  

Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim cmd23 As New SqlCommand("IBS_TransactionSummary", con.connect)
cmd23.CommandType = CommandType.StoredProcedure
cmd23.Parameters.Add("@startDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = startdate
cmd23.Parameters.Add("@endDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = enddate
' *** Supply the DataTable as a parameter to the procedure here: *** '
Dim tvp1 As SqlParameter =cmd23.Parameters.AddWithValue("@location", locTable)
tvp1.SqlDbType = SqlDbType.Structured
tvp1.TypeName = "dbo.Loc"
da.SelectCommand = cmd23
da.Fill(ds)