2011-07-29 59 views

回答

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

+0

謝謝guys..I將嘗試這些,讓你知道 – DQELER

+0

datatable在哪裏? – clweeks

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 

這只是需要從表中的ID這是從SQL Server拉,每個ID添加它到列表(整數),然後在你的存儲過程只需添加一個XML數據類型的數據來的列表。

+0

比ks guys..I將嘗試這些,並讓你知道 – DQELER

相關問題