Results 1 to 5 of 5

Thread: passing parameters to a stored procedure

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    166

    passing parameters to a stored procedure

    I am confused about how to pass parameters to a stored procedure. The stored procedure is written in sql server 2005 and the code is in vb.net.

    I have included the sub that I am working on. I know that my use of parameters is wrong. Can someone explain what I should do?

    Code:
    Public Sub AddRecipe()
    
            Using myDBconnection As New SqlConnection(setConnstring)
                Using SQLcmd As New SqlCommand()
                    SQLcmd.Connection = myDBconnection
                    SQLcmd.CommandType = CommandType.StoredProcedure
                    SQLcmd.CommandText = "CreateRecipe"
                   SQLcmd.Parameters.Add("Name", NVarChar)
    
    
                End Using
            End Using
        End Sub

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: passing parameters to a stored procedure

    Here is a good example on how to use StoredProcedures in .Net.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: passing parameters to a stored procedure

    There are essentially two ways to add a parameter to a command and they are the same whether you're executing a stored procedure or inline SQL. If you're calling ExecuteScalar, ExecuteReader or ExecuteNonQuery on the command or if you're executing it as the SelectCommand of a DataAdapter by calling Fill then you will normally want to set the parameter value when you add the parameter, e.g.
    vb.net Code:
    1. myCommand.Parameters.AddWithValue("@ParamName", paramValue)
    There's no need to specify the type or size of the parameter because that's inferred from the value.

    If you're executing the command as the DeleteCommand, InsertCommand or UpdateCommand of a DataAdapter by calling Update then you'll want to specify the source column for the parameter from the DataTable being saved, e.g.
    vb.net Code:
    1. myCommand.Parameters.Add("@ParamName", SqlDbType.NVarChar, 50, "SourceColumnName")
    In this case you need to specify the type of the parameter and the size for those types that require it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    166

    Re: passing parameters to a stored procedure

    thanks

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    166

    Re: passing parameters to a stored procedure

    Can someone point me to resources on the syntax for stored procedures in sql server 2005. Its been a while since I have written a stored procedure and I am rusty.

    I also need resources on how to pass an array of values all of the same data type to this stored procedure.

    thanks,

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