Use ADO. Net to operate SQL Server Stored Procedures

Source: Internet
Author: User
1. Call the stored procedure without input/output parameters
Create procedure testproc
As
Select pub_id, title_id, price, pubdate
From titles
Where price is not null
Order by pub_id

Use the following program to call this stored procedure. When calling this stored procedure, you must tell the command object that you want to call the stored procedure. <Form ID = "form1" runat = "server">
<Div>
<Asp: DataGrid id = "DG" runat = "server"/>
</Div>
</Form>

Sqlconnection conn;
Protected void page_load (Object sender, eventargs E)
{
Conn = new sqlconnection ("Server = localhost; database = pubs; uid = sa; Pwd = ''");
Sqlcommand comm = new sqlcommand ("testproc", Conn );
Comm. commandtype = commandtype. storedprocedure;
Conn. open ();
Sqldatareader DR = comm. executereader ();
DG. datasource = Dr;
DG. databind ();
Conn. Close ();
}

2. Create procedure sp_checkpass
(@ Chkname varchar (30), @ chkpass varchar (30), @ isvalid varchar (12) Output)
As
If exists (select username from webusers where username = @ chkname and
Userpass = @ chkpass)
Select @ isvalid = 'good'
Else
Select @ isvalid = 'bad'

Sqlconnection conn;
Protected void page_load (Object sender, eventargs E)
{
Conn = new sqlconnection ("Server = localhost; database = pubs; uid = sa; Pwd = ''");
Sqlcommand comm = new sqlcommand ("sp_checkpass", Conn );
Comm. commandtype = commandtype. storedprocedure;
Sqlparameter parm = comm. Parameters. Add ("@ chkname", sqldbtype. varchar, 30 );
Parm. value = "AA ";
Parm = comm. Parameters. Add ("@ chkpass", sqldbtype. varchar, 30 );
Parm. value = "AA ";
Parm = comm. Parameters. Add ("@ isvalid", sqldbtype. varchar, 12 );
Parm. Direction = parameterdirection. output;

Conn. open ();
Sqldatareader DR = comm. executereader ();
Response. Write (Comm. Parameters ["@ isvalid"]. value );
Conn. Close ();
}

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.