How to use stored procedures in C # (SQL Server 2000)

Source: Internet
Author: User

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

Related Article

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.