To use the stored procedure in C #, first check the SQL statement for creating the test table:
create table test55
(
uid int identity(1,1),
class1 varchar(20),
class2 varchar(20),
uname varchar(20),
birth smalldatetime,
meno varchar(50)
)
alter table test55
add constraint primary_id primary key(uid)
Create a stored procedure with input, output, and return value parameters:
Create proc proc_out @ UUID int, @ output varchar (200) output
As
-- Select result set
Select * from test where uid> @ uid
-- Assign values to output parameters
Set @ output = Total number of records: + convert (varchar (10), (select count (*) from test ))
-- Return is used to return a value to the stored procedure.
Return 200;
Go
Use stored procedures in C:
Use SQL statements with Parameters
Private void SQL _param ()
{
SqlConnection conn = new SqlConnection ("server =.; uid = sa; pwd = 1234; database = china ");
// The @ myid parameter is introduced in the SQL statement.
String SQL = "select * from test where uid> @ myid ";
SqlCommand comm = new SqlCommand (SQL, conn );
// Use the add method of the Parameters attribute of comm to define and assign values to the above @ myid Parameter
// The SqlDbType class provides the same database type as the SqlServer Data Type
SqlParameter sp = comm. Parameters. Add ("@ myid", SqlDbType. Int );
Sp. Value = 10; // assign a Value to the input parameter
// The default execution method of the Command object is Text. You can also skip the next sentence.
Comm. CommandType = CommandType. Text;
// Upload the Command object as a parameter of DataAdapter
SqlDataAdapter da = new SqlDataAdapter (comm );
DataSet ds = new DataSet ();
Da. Fill (ds );
// Bind data to the DataGrid1 Control
This. DataGrid1.DataSource = ds;
This. DataGrid1.DataBind ();
}
Standard Edition for Stored Procedures
private void sql_proc()
{
SqlConnection