[Visual Studio] ExecuteReader's return value binding GridView

Source: Internet
Author: User

First you have to get an instance of SqlConnection object con, which is used below (as for how to connect the database, I don't need to say it)
SQL statement

SqlCommand cmd = new SqlCommand ("SELECT * from Bbsboards where boardsortid=1", con);

SqlDataAdapter ad = new SqlDataAdapter ("select * from Bbsboards where boardsortid=1", con);
Ad. Fill (DS);


Stored Procedures
(1) No parameters
SqlCommand cmd = new SqlCommand ("Getboards", con);
Cmd.commandtype = CommandType.StoredProcedure;


(2) parameters (with input parameters, output parameters and two return result sets)
SqlCommand cmd = new SqlCommand ("Getboardsbysortid", con);
Cmd.commandtype = CommandType.StoredProcedure;
Cmd. Parameters.Add (New SqlParameter ("@sortid", 1));


Set OUTPUT parameters
SqlParameter sp = new SqlParameter ();
Sp. DbType = Dbtype.int32;
Sp. Direction = ParameterDirection.Output;
Sp. ParameterName = "@a";
Cmd. Parameters.Add (SP);


Set return value
SqlParameter p = new SqlParameter ();
P.dbtype = Dbtype.int32;
P.direction = ParameterDirection.ReturnValue;
Cmd. Parameters.Add (P);



SqlDataReader ad = cmd. ExecuteReader ();



This. Gridview1.datasource = AD; Here, I bind the result set directly to the GridView.

This. Gridview1.databind ();

Return multiple result sets
Ad. NextResult ();
This. Gridview2.datasource = AD; Here, I bind the result set directly to the GridView.

This. Gridview2.databind ();

Con. Close ();

Get return value (must be after con.close ())

P.value.tostring ();


Get output parameter values
      sp. Value.tostring ();

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.