将数据表作为参数传递给VB.net中的SQL Server存储过程

时间:2011-07-29 16:51:44

标签: vb.net stored-procedures datatable

请建议如何执行此操作。感谢。

3 个答案:

答案 0 :(得分:0)

阅读本文,了解如何执行此操作Passing an array or DataTable into a stored procedure

答案 1 :(得分:0)

将其作为XML数据类型传递,几个月前我就这么做了。所以当我找到一些处理它的代码时,我会重新编辑。

Private Function AddToList(dtData As DataTable) As List(Of [Integer])
Dim ListOfInt As New List(Of Integer)()
For Each row As DataRow In dtData.Rows
    For Each Col As DataColumn In dtData.Columns
        ListOfInt.Add(row(Col).ToString())
    Next
Next
Return ListOfInt
End Function
Private Function DataToXML() As XDocument
        Dim DataDoc As XDocument = <?xml version='1.0'?>
                                   <Root>
                                       <%= RenderKeys(SelectedDataValues) %>
                                   </Root>
        Return DataDoc
End Function    
Private Function RenderKeys(ByVal keys As List(Of Integer)) As Collection(Of XElement)
        Dim ElementCollection As New Collection(Of XElement)
        For Each Key As Integer In keys
            Dim XKey As XElement = <Key ID=<%= Key %>/>
            ElementCollection.Add(XKey)
        Next
        Return ElementCollection
End Function

这只是从一个从SQL Server中提取的表中的ID列表,并将其每个id添加到一个列表中(整数)然后在你的sproc中为刚进入的数据添加一个xml数据类型。

答案 2 :(得分:0)

PrivateFunction GetDataFromDb(ByVal lcSQL AsString, ByVal loCommandType As CommandType, _
    ByVal lcTableName AsString, ByValParamArray loParameters() As SqlParameter) As DataSet

    Dim loResult As DataSet
    Dim loConnection As SqlConnection
    Dim loCommand As SqlCommand
    Dim loAdapter As SqlDataAdapter
    Dim i As Int32
    Dim loParameter As SqlParameter

    Try

        'Create and open connection to the Northwind database
        loConnection = New SqlConnection("Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=(local);Connect Timeout=30")
        loConnection.Open()

        'Prepare command and to select data from the database
        loCommand = New SqlCommand(lcSQL, loConnection)
        loCommand.CommandType = loCommandType

        IfNot loParameters IsNothingThen
            ForEach loParameter In loParameters
                loCommand.Parameters.Add(loParameter)
            Next
        EndIf

        loAdapter = New SqlDataAdapter(loCommand)

        loResult = New DataSet
        loAdapter.Fill(loResult, lcTableName)

        'Return list of the customers as a DataSet
        Return loResult

    Catch ex As Exception
        Throw ex
    Finally

        'Clean resources
        IfNot loAdapter IsNothingThen
            loAdapter.Dispose()
            loAdapter = Nothing
        EndIf

        IfNot loCommand IsNothingThen
            loCommand.Dispose()
            loCommand = Nothing
        EndIf

        IfNot loConnection IsNothingThen

            If loConnection.State = ConnectionState.Open Then
                loConnection.Close()
            EndIf

            loConnection.Dispose()
            loConnection = Nothing

        EndIf
    EndTry

EndFunction

发现于:http://support.microsoft.com/kb/555266