Results 1 to 7 of 7

Thread: stored procedure (parameter)

  1. #1

    Thread Starter
    Lively Member afterMoon's Avatar
    Join Date
    Oct 2002
    Location
    cochin
    Posts
    117

    stored procedure (parameter)

    hi
    is there a way for giving parameters to a stored procedure without using command.parameters.add method?. In vb6, we could've given parameters directly with connection.execute.

    please help
    when in doubt, win the trick

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    VB Code:
    1. Dim cmd As SqlCommand = New SqlCommand("Exec usp_Authors_f @au_id='123-56-1234'")

  3. #3

    Thread Starter
    Lively Member afterMoon's Avatar
    Join Date
    Oct 2002
    Location
    cochin
    Posts
    117
    hi
    thanks. but it gave an sqlexception error.
    any suggetions?

    thanks
    when in doubt, win the trick

  4. #4
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    hi,

    it should be:

    VB Code:
    1. Dim cmd As SqlCommand = New SqlCommand("Exec usp_Authors_f @au_id='123-56-1234'", MyConnection)

  5. #5

    Thread Starter
    Lively Member afterMoon's Avatar
    Join Date
    Oct 2002
    Location
    cochin
    Posts
    117
    hi
    ofcourse I tried that. Then I got that error.
    any idea?
    when in doubt, win the trick

  6. #6
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    um, i only posted the part you needed, hence, no connection information, you don't have to put the connection reference in the initializer(you can do that later).
    VB Code:
    1. Imports System
    2. Imports System.Data
    3. Imports System.Data.SqlClient
    4. Public Module TestCommand
    5.     Public Sub Main()
    6.         Dim connString As String = "user id=sa;password=sa;database=pubs;server=Manson;"
    7.         Dim cn As SqlConnection = New SqlConnection(connString)
    8.         Dim cmdText As String = "Exec usp_Author_f '274-80-9391'"
    9.         Dim cmd As SqlCommand = New SqlCommand(cmdText, cn)
    10.         Dim dr As SqlDataReader
    11.         Try
    12.             cmd.Connection.Open()
    13.             dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    14.             If dr.Read() Then
    15.                 Console.WriteLine("Author's last name is {0}", dr("au_lname"))
    16.             End If
    17.         Catch sqlEx As SqlException
    18.             Console.WriteLine("Sql Exception: " & sqlEx.Message)
    19.         Catch ex As Exception
    20.             Console.WriteLine("Exception: " & ex.Message)
    21.         Finally
    22.             If Not dr Is Nothing Then
    23.                 dr.Close()
    24.             End If
    25.             cmd.Dispose()
    26.             cn.Dispose()
    27.         End Try
    28.         Console.WriteLine("Press <Enter> to exit...")
    29.         Console.ReadLine()
    30.     End Sub
    31. End Module
    Here's the stored proc i'm calling:
    Code:
    Create Procedure usp_Author_f
    	@au_id id
    As
    Set Nocount On
    Select 
    	* 
    From 
    	Authors
    Where 
    	au_id=@au_id
    Set Nocount Off
    go

  7. #7

    Thread Starter
    Lively Member afterMoon's Avatar
    Join Date
    Oct 2002
    Location
    cochin
    Posts
    117
    Thank you very much Mr.PVB

    the problem was I gave command.commandType as commandType.StoredProcedure. it's only text. right?

    Thanks again
    when in doubt, win the trick

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