C # Call stored procedure Simple complete example

Source: Internet
Author: User
Tags rowcount
Stored Procedures

CREATE PROC P_test
@Name VARCHAR (20),
@Rowcount INT OUTPUT
As
BEGIN
SELECT * from T_customer WHERE name= @Name
SET @Rowcount =@ @ROWCOUNT
End
Go
----------------------------------------------------------------------------------------
--stored procedure calls are as follows:
----------------------------------------------------------------------------------------
DECLARE @i INT
EXEC p_test ' A ', @i OUTPUT
SELECT @i
--The result
/*
Name Address Tel
---------- ---------- --------------------
A Address Telphone
(The number of rows affected is 1 rows)


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

(The number of rows affected is 1 rows)
*/
----------------------------------------------------------------------------------------
--dotnet section (C #)
--webconfig file:
----------------------------------------------------------------------------------------
......
</system.web>

<!--database connection string
-->
<appSettings>
<add key= "connectstring" value= "server=" (local); User Id=sa; password=;d atabase=test "/>
</appSettings>

</configuration>
----------------------------------------------------------------------------------------
--c# code: (used with two test controls, DATAGRID1 (for displaying the binding result set), lable (for displaying stored procedure return single values)
----------------------------------------------------------------------------------------
Add a Database Reference
Using System.Data.SqlClient;
......
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
String Dbconnstr;
DataSet mydataset=new DataSet ();
System.Data.SqlClient.SqlDataAdapter dataadapter=new System.Data.SqlClient.SqlDataAdapter ();
dbconnstr=system.configuration.configurationsettings.appsettings["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, assign values
MYCOMMAND.PARAMETERS.ADD ("@Name", SqlDbType.VarChar);
mycommand.parameters["@Name"]. Value = "A";

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 ();
Get stored procedure output parameters
label1.text=mycommand.parameters["@Rowcount"]. Value.tostring ();

if (myConnection.State = = ConnectionState.Open)
{
Myconnection.close ();
}

}
----------------------------------------------------------------------------------------
Run the above code (returns the record collection and stored procedure return values)




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.