| |
|
How to run a stored procedure from .NET code?
Use the Command object and its ExecuteNonQuery method to run a DDL or DCL statement of SQL.
First, create a connection object and a command object, and configure these objects for the
statement you wish to execute.
Open the database connection. Call ExecuteNonQuery on the command. Close the connection.
Example
Suppose we have to insert FirstName into a table t_Employees. Our Store procedure is:
Create Procedure dbo.InsertFirstName
(
@FirstName varchar(30)
)
as
Insert Into t_Employees values (@FirstName)
VB.NET code below, to pass FirstName to this Stored Procedure
Dim cmRunProc as New SqlCommand("dbo.InsertFirstName", objCon)
cmRunProc.CommandType = CommandType.StoredProcedure
cmRunProc.Parameters.Add("@FirstName", txtFirstName.Text)
objCon.Open()
cmdRunProc.ExecuteNonQuery()
objCon.Close()
ADO.NET
Connection Object
Command Object
SelectCommand
SqlCommandBuilder
DataView
DataRelation
Diffgram
Typed DataSet
Call Stored Procedure
More Interview Questions...
| |