235098b2735
235098b2735

Reputation: 3

When executing stored procedures in VB.NET/SQL, are we supposed to add "EXEC" or not?

I'm not sure what to believe anymore. When we are running our VB.NET SqlCommand off of stored procedures, do we add EXEC in our commands or not?!

I get an error:

Could not find stored procedure 'EXEC uspGrabAutoByYMM'.

But then other people tell me you MUST put EXEC in there for it to run.

Here's my sample code:

Public Sub BindGridAutosYMM()
    Dim constring As String = "server=classified;database=classified"
    Using con As New SqlConnection(constring)
        Using cmd As New SqlCommand("EXEC uspGrabAutoByYMM", con)
            cmd.Parameters.Add("@Year", SqlDbType.VarChar).Value = TextBox1.Text
            cmd.Parameters.Add("@Make", SqlDbType.VarChar).Value = TextBox2.Text
            cmd.Parameters.Add("@Model", SqlDbType.VarChar).Value = TextBox3.Text
            cmd.CommandType = CommandType.StoredProcedure
            Using sda As New SqlDataAdapter(cmd)
                Using dt As New DataTable()
                    sda.Fill(dt)
                    DataGridView1.DataSource = dt
                End Using
            End Using
        End Using
    End Using
End Sub

Upvotes: 0

Views: 153

Answers (1)

Vinicius Gonçalves
Vinicius Gonçalves

Reputation: 2724

No.

System.Data.CommandType.StoredProcedure does it for you.

It will be helpful: How to: Execute a Stored Procedure that Returns Rows

See too:

Using EXECUTE with Stored Procedures You do not have to specify the EXECUTE keyword when you execute stored procedures when the statement is the first one in a batch.

EXECUTE (Transact-SQL)

If you remove the "exec" and the problem persists, confirm that this procedure exists in your database.

Upvotes: 2

Related Questions