1

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

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 窗体

4

3 回答 3

1

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

最简单的事情似乎是DataTable用用户选择的值填充 a 并将其传递给@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)
于 2013-11-04T08:02:07.240 回答
0

表类型 SQL SERVER 2008

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

于 2013-11-04T07:43:06.810 回答
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)
于 2013-11-04T16:26:00.217 回答