Running a Stored Procedure from VB.NET

For general computer tips and techniques which can help anyone from newbies to experienced users get more out of their computers, check out Computer Secrets Unleashed.
 This example executes a Stored Procedure when you load a form. This example uses a Data Grid to store the results.

  1. 'Create a SqlCommand to represent the query
    Dim cmd As SqlCommand = SqlConnection1.CreateCommand
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = txtQuery.Text
  2. 'Create a SqlDataAdapter to talk to the database
    Dim da As SqlDataAdapter = New SqlDataAdapter
    da.SelectCommand = cmd
  3. 'Create a DataSet to hold the results
    Dim ds As DataSet = New DataSet
    'Fill the Dataset
    da.Fill(ds, "Results")
  4. 'And bind it to the DataGrid
    dgResults.DataSource = ds
    dgResults.DataMember = "Results"


Share this article!

Follow us!

Find more helpful articles: