Results 1 to 9 of 9

Thread: Passing parameters to a stored proc

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    Fort Lauderdale, FL
    Posts
    98

    Passing parameters to a stored proc

    OK, this may be a silly question, but I'm all about saving time and space in my code.

    Right now this is what my code looks like to insert a record thru a sp:
    cn.Open()
    cmd.Connection = cn
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "sp_Request"

    With cmd
    .Parameters.Add("@Request", SqlDbType.VarChar, 200)
    .Parameters("@Request").Direction = ParameterDirection.Input
    .Parameters("@Request").Value = getTypes()
    .Parameters.Add("@Rq_No", SqlDbType.VarChar, 200)
    .Parameters("@Rq_no").Direction = ParameterDirection.Input
    .Parameters("@Rq_no").Value = getPermitNumber()
    End With

    Is there a more compact way of doing this?
    I'm still feeling my way thru .net.

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    VB Code:
    1. Dim cn As New SqlConnection("YourConnectionString")
    2. Dim cmd As New SqlCommand("sp_Request")
    3. cmd.Connection = cn
    4. cmd.CommandType = CommandType.StoredProcedure
    5. cmd.Parameters.Add(New SqlParameter("@Request", getTypes()))
    6. cmd.Parameters.Add(New SqlParameter("@Rq_No", getPermitNumber()))
    7. cmd.Connection.Open()

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Hey pvb did that work for you?

    I saw that in the Help docs and tried it a while back but if I don't specify the datatype then it doesn't work for me.

    As for shortening that, if pvb's way (which should work but doesn't for me) doesn't work then you can drop the direction because by default its input. And even though I have to specify datatype for mine to work I don't have to specify length.

  4. #4
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Show the definition of the stored proc, cuz you don't need to specify the datatype.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is the parameter part:

    Alter PROCEDURE sel_User
    @LoadBy as int=0, /*determines what to use to load by*/
    @UserID as int=0, /*holds the ID if used otherwise ignored*/
    @Username as varchar(255), /*holds username otherwise ignored*/
    @Password as varchar(255) /*holds password otherwise ignored*/
    AS

    I'll have to try again because I don't remember if it just didn't accept my parameter value or if it gave an error.

  6. #6
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    problem might be how you're setting up those params in your stored proc, the following works fine:

    VB Code:
    1. Private Sub testCmd()
    2.     Dim dr As SqlDataReader
    3.     Dim connString As String = System.Configuration.ConfigurationSettings.AppSettings.Item("DBConnString").ToString()
    4.     Dim cn As New SqlConnection(connString)
    5.     Dim cmd As New SqlCommand("sel_User")
    6.     cmd.Connection = cn
    7.     cmd.CommandType = CommandType.StoredProcedure
    8.     cmd.Parameters.Add(New SqlParameter("@LoadBy", 5))
    9.     cmd.Parameters.Add(New SqlParameter("@UserID", 2))
    10.     cmd.Parameters.Add(New SqlParameter("@UserName", "some name"))
    11.     cmd.Parameters.Add(New SqlParameter("@Password", "my password"))
    12.     cmd.Connection.Open()
    13.     dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    14.     If (dr.Read()) Then
    15.         LoadBy.Text = dr("LoadBy").ToString() & ""
    16.         UserId.Text = dr("UserId").ToString() & ""
    17.         UserName.Text = dr("UserName").ToString() & ""
    18.         Password.Text = dr("Password").ToString() & ""
    19.     End If
    20. End Sub

    SQL:
    Code:
    Create Procedure sel_User
    	  @LoadBy int = 0
    	, @UserID int = 0
    	, @Username varchar ( 255 )
    	, @Password  varchar ( 255 )
    As
    Set Nocount On
    Select 
    	  @LoadBy As 'LoadBy'
    	, @UserID As 'UserID'
    	, @Username As 'Username'
    	, @Password As 'Password'
    Set Nocount Off
    go

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Is that normal use teh non @ name and then reassign it? I've always just used the @LoadBy or whatnot directly. Actually this isn't the sp that gave me the trouble and I started to retest and shorten things up a bit but ran out of time. Although it let me do it your way with @LoadBy so maybe it was something else in 1 particular sp??? I'll test more tomorrow.

  8. #8
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    In response to the original query this is how I add a parameter:-

    cmd.parameters.add(new sqlparamater("@someid", sqldbtype.int)).value = someint

    This makes the code nice and short, allows me to define the type and specify the value all in one line and I only need another line if I need to specify direction, which is rarely.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    Fort Lauderdale, FL
    Posts
    98

    Thanks!!!

    Thanks, that speeds up coding tremedously.

    But now you all can tease me for not continuing to use sourcesafe or some other backup. I just lost my whole project.

    Oh well, this is what turns me into a better programmer, right?

    :-)

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