1. Call the stored procedure, but no return value
CopyCode The Code is as follows: Private function sqlproc1 (byval procname as string) as Boolean
'The Data Link part is omitted, and myconn is the linked object procname is the stored procedure name.
Dim mycommand as new sqlclient. sqlcommand (procname, myconn)
With mycommand
. Commandtype = commandtype. storedprocedure
. Parameters. Add ("@ codetype", sqldbtype. varchar, 20). value = "grade code"
Try
. Executenonquery ()
Return true
Catch ex as exception
Return false
End try
End Function
2. Call the stored procedure and return normal values. Copy codeThe Code is as follows: Private function sqlproc1 (byval procname as string) as string
'The Data Link part is omitted, and myconn is the link object.
Dim mycommand as new sqlclient. sqlcommand (procname, myconn)
With mycommand
. Commandtype = commandtype. storedprocedure
. Parameters. Add ("@ codetype", sqldbtype. varchar, 20). value = "grade code"
. Parameters. Add ("@ newcode", sqldbtype. varchar, 20). Direction = parameterdirection. Output
Try
. Executenonquery ()
Return. parameters (1). Value ()
Catch ex as exception
Return "generate without encoding"
End try
End Function
3. Call the stored procedure and return the dataset
'Vb. Net code Copy code The Code is as follows: Private function sqlproc2 (byval procname as string, byval param1 as string) as Dataset
'Define the command object and use the Stored Procedure
Dim mycommand as new sqlclient. sqlcommand
Mycommand. commandtype = commandtype. storedprocedure
Mycommand. commandtext = procname
Mycommand. Connection = myconn
'Define a data adapter and set parameters
Dim mydapter as new sqlclient. sqldataadapter (mycommand)
Mydapter. selectcommand. Parameters. Add ("@ name", sqldbtype. varchar, 20). value = param1
'Define A DataSet object and fill the dataset
Dim mydataset as new dataset
Try
Mydapter. Fill (mydataset)
Catch ex as exception
End try
Return mydataset
End Function
'Stored Procedure Code
Create proc test @ name varchar (20)
Select * From ec_grade where cgradename = @ name
Go
* ** If you modify part of the stored procedure, you can use it as a query.
Create proc Test
@ Name varchar (200) =''
-- Note that 200 is the length of the query condition, which can be determined based on the actual situation. However, it is not recommended to use it for long query conditions.
As
Declare @ sql1 varchar (8000)
If @ name <>''
Select @ sql1 = 'select * From ec_grade where '+ @ name
Else
Select @ sql1 = 'select * From ec_grade'
Exec (@ sql1)
Go