ASP.net stored Procedure Call _ Practical Tips

Source: Internet
Author: User
1. Call stored procedure, but no return value
Copy Code code as follows:

Private Function SqlProc1 (ByVal procname as String) as Boolean
' Defines the data link part omitted, myconn for the link object procname for 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 stored procedure, return normal value
Copy Code code as follows:

Private Function SqlProc1 (ByVal procname As String) as String
' Define data Link part omitted, myconn as linked 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 "No Code generation"
End Try
End Function

3. Calling a stored procedure, returning a dataset
' VB. NET code
Copy Code code 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 the parameters
Dim Mydapter as New sqlclient.sqldataadapter (mycommand)
MYDAPTER.SELECTCOMMAND.PARAMETERS.ADD ("@name", SqlDbType.VarChar, 20). Value = Param1
' Define a DataSet object and populate 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 AS
Select * from Ec_grade where cgradename= @name
Go
If you modify a stored procedure to some content, you can use it as a query
CREATE Proc Test
@name varchar (200) = '
--Here you should note that 200 is the length of the query conditions, depending on the actual situation, but not recommended for too 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.