PDA

Click to See Complete Forum and Search --> : stored procedure (parameter)


afterMoon
Nov 19th, 2002, 04:22 AM
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

pvb
Nov 19th, 2002, 09:37 AM
Dim cmd As SqlCommand = New SqlCommand("Exec usp_Authors_f @au_id='123-56-1234'")

afterMoon
Nov 20th, 2002, 03:20 AM
hi
thanks. but it gave an sqlexception error.
any suggetions?

thanks

Marivic
Nov 20th, 2002, 03:28 AM
hi,

it should be:


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

afterMoon
Nov 20th, 2002, 03:55 AM
hi
ofcourse I tried that. Then I got that error.
any idea?

pvb
Nov 20th, 2002, 09:57 AM
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).

Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Module TestCommand
Public Sub Main()
Dim connString As String = "user id=sa;password=sa;database=pubs;server=Manson;"
Dim cn As SqlConnection = New SqlConnection(connString)
Dim cmdText As String = "Exec usp_Author_f '274-80-9391'"
Dim cmd As SqlCommand = New SqlCommand(cmdText, cn)
Dim dr As SqlDataReader
Try
cmd.Connection.Open()
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If dr.Read() Then
Console.WriteLine("Author's last name is {0}", dr("au_lname"))
End If
Catch sqlEx As SqlException
Console.WriteLine("Sql Exception: " & sqlEx.Message)
Catch ex As Exception
Console.WriteLine("Exception: " & ex.Message)
Finally
If Not dr Is Nothing Then
dr.Close()
End If
cmd.Dispose()
cn.Dispose()
End Try
Console.WriteLine("Press <Enter> to exit...")
Console.ReadLine()
End Sub
End Module
Here's the stored proc i'm calling:

Create Procedure usp_Author_f
@au_id id
As
Set Nocount On
Select
*
From
Authors
Where
au_id=@au_id
Set Nocount Off
go

afterMoon
Nov 21st, 2002, 12:02 AM
Thank you very much Mr.PVB

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

Thanks again