Asp.net call stored procedure-simple Edition

Source: Internet
Author: User

-- The simple stored procedure is as follows:
Bytes ----------------------------------------------------------------------------------------
Create proc p_test
@ Name varchar (20 ),
@ Rowcount int output
As
Begin
Select * From t_customer whereName = @ name
Set @ rowcount = @ rowcount
End
Go
Bytes ----------------------------------------------------------------------------------------
-- The stored procedure is called as follows:
Bytes ----------------------------------------------------------------------------------------
Declare @ I int
Exec p_test 'A', @ I output
Select @ I
-- Result
/*
Name Address Tel
----------------------------------------
A address telphone

(The number of affected rows is 1)


-----------
1

(The number of affected rows is 1)
*/
Bytes ----------------------------------------------------------------------------------------
-- DOTNET part (C #)
-- Webconfig file:
Bytes ----------------------------------------------------------------------------------------
......
</System. Web>

<! -- Database connection string
-->
<Deleetask>
<Add key = "connectstring" value = "Server = (local); User ID = sa; Password =; database = test"/>
</Appsettings>

</Configuration>
Bytes ----------------------------------------------------------------------------------------
-- C # code: (two test controls are used: datagrid1 (used to display the binding result set) and lable (used to display the single value returned by the stored procedure)
Bytes ----------------------------------------------------------------------------------------
// Add database reference
Using system. Data. sqlclient;
......
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
String dbconnstr;
Dataset mydataset = new dataset ();
System. Data. sqlclient. sqldataadapter dataadapter = new system. Data. sqlclient. sqldataadapter ();
Dbconnstr = system. configuration. configurationsettings. deleettings ["connectstring"];
System. Data. sqlclient. sqlconnection myconnection = new system. Data. sqlclient. sqlconnection (dbconnstr );
If (myconnection. State! = Connectionstate. open)
{
Myconnection. open ();
}
System. Data. sqlclient. sqlcommand mycommand = new system. Data. sqlclient. sqlcommand ("p_test", myconnection );
Mycommand. commandtype = commandtype. storedprocedure;
// Add input query parameters and assign values
Mycommand. Parameters. Add ("@ name", sqldbtype. varchar );
Mycommand. Parameters ["@ name"]. value = "";

// Add output parameters
Mycommand. Parameters. Add ("@ rowcount", sqldbtype. INT );
Mycommand. Parameters ["@ rowcount"]. Direction = parameterdirection. output;

Mycommand. executenonquery ();
Dataadapter. selectcommand = mycommand;

If (mydataset! = NULL)
{
Dataadapter. Fill (mydataset, "table ");
}

Datagrid1.datasource = mydataset;
Datagrid1.databind ();
// Obtain the Stored Procedure output parameters
Label1.text = mycommand. Parameters ["@ rowcount"]. value. tostring ();

If (myconnection. State = connectionstate. open)
{
Myconnection. Close ();
}

}

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.