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
Printable View
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
VB Code:
Dim cmd As SqlCommand = New SqlCommand("Exec usp_Authors_f @au_id='123-56-1234'")
hi
thanks. but it gave an sqlexception error.
any suggetions?
thanks
hi,
it should be:
VB Code:
Dim cmd As SqlCommand = New SqlCommand("Exec usp_Authors_f @au_id='123-56-1234'", MyConnection)
hi
ofcourse I tried that. Then I got that error.
any idea?
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).
Here's the stored proc i'm calling:VB Code:
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
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
Thank you very much Mr.PVB
the problem was I gave command.commandType as commandType.StoredProcedure. it's only text. right?
Thanks again