Results 1 to 5 of 5

Thread: Adding Parameters to Stored Procedures

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    12

    Adding Parameters to Stored Procedures

    Hello
    I'm a bit of a beginer with VB.net and am having some bother adding parameters to a stored procedure I need to execute. I want to execute a stored procedure that in SQL I can do using

    exec sp_spaceused Customer

    Not sure how to do this in VB.net. To get the stored proc running I'm doing the following

    cStored.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
    cStored.CommandText = "sp_spaceused"
    cStored.ActiveConnection = cConnection

    recRS = cStored.Execute

    Can someone please tell me how I add the Customer parameter.

    Cheers

    Steve
    Last edited by stevegreen; Jul 23rd, 2004 at 10:08 AM.

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Have not tried it, but MSDN has an example of how to do. Search for "Using Stored Procedures with a Command".

    Here's a snippet, copied right from their example. Looks like you add a paramater to the command, then set the value:

    VB Code:
    1. Dim myParm As OleDbParameter = salesCMD.Parameters.Add("@CategoryName", OleDbType.VarChar, 15)
    2. myParm.Value = "Beverages"

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Posts
    12
    I saw similar code on the net when I was looking, but using this code is not working. Do I need to add some reference or something?

    Steve

  4. #4
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Can you give more details on what you mean by "not working"? Syntax error? Exception? This code works, although I'm using MSSQL:

    VB Code:
    1. Dim con As SqlConnection = New SqlConnection("Persist Security Info=False;Integrated Security=SSPI;database=Patriot;server=MIKELAP")
    2.         Dim cmd As SqlCommand = New SqlCommand("sp_spaceused", con)
    3.         cmd.CommandType = CommandType.StoredProcedure
    4.         Dim myParam As SqlParameter = cmd.Parameters.Add("@objname", SqlDbType.NVarChar, 20)
    5.         myParam.Value = "Individuals"
    6.         con.Open()
    7.         Dim myReader As SqlDataReader = cmd.ExecuteReader
    8.         Do While myReader.Read
    9.             MessageBox.Show("Rows: " & myReader.GetString(1))
    10.         Loop
    11.         myReader.Close()
    12.         con.Close()

  5. #5
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    first post of mike you need imports system.data.oledb and the secod, you need imports system.data.sqlclient.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width