DAL: Retrieve a DataTable using a Stored Procedure
http://msmvps.com/blogs/deborahk/archive/2009/07/07/dal-retrieve-a-datatable-using-a-stored-procedure.aspxCode:
Public Class Dac
Public Shared Function ExecuteDataTable(ByVal storedProcedureName _
As String, _
ByVal ParamArray arrParam() As SqlParameter) As DataTable
Dim dt As DataTable
' Open the connection
Using cnn As New SqlConnection(
"Data Source=.\sqlexpress;Initial Catalog=AcmeRentals;
Integrated Security=True;")
cnn.Open()
' Define the command
Using cmd As New SqlCommand
cmd.Connection = cnn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = storedProcedureName
' Handle the parameters
If arrParam IsNot Nothing Then
For Each param As SqlParameter In arrParam
cmd.Parameters.Add(param)
Next
End If
' Define the data adapter and fill the dataset
Using da As New SqlDataAdapter(cmd)
dt = New DataTable
da.Fill(dt)
End Using
End Using
End Using
Return dt
End Function
End Class