Return SqlDataReader data in C #/Mssql/winform/asp.net-sqlhelper

Source: Internet
Author: User

        <summary>//Execute a SqlCommand that returns a resultset against the database specified in the        Connection string///using the provided parameters. </summary>//<param name= "connectionString" > a valid database connection string </param>///<param Name = "Cmdtype" >sqlcommand command type (stored procedure, T-SQL statement, and so on. ) </param>//<param name= "cmdtext" > stored procedure name or T-SQL statement </param>//<param name= "command Parameters "> provides a list of the parameters used in the SqlCommand command as an array </param>//<returns>a SqlDataReader containing the results& lt;/returns> public static SqlDataReader ExecuteReader (String connectionString, CommandType cmdtype, String CMDT            EXT, params sqlparameter[] commandparameters) {SqlCommand cmd = new SqlCommand ();            SqlConnection conn = new SqlConnection (connectionString);             We use a try/catch here because if the method throws an exception we want toClose the connection throw code, because no DataReader would exist, hence the//Commandbehaviour.closeconne Ction won't work try {PrepareCommand (CMD, conn, null, Cmdtype, Cmdtext, COMMANDPA                Rameters); SqlDataReader rdr = cmd. ExecuteReader (commandbehavior.closeconnection); Cmd.                Parameters.clear ();            return RDR; } catch {Conn.                Close ();            Throw }} #region//executedataset method

  

================================================================================================

C # Database access issues with using using when the return value type is SqlDataReader
2014-04-19 of 0 Reviews Source: C # Database access problems with using using when the return value type is SqlDataReader
I want to contribute

When encapsulating the universal SQL Server data accessible method, if the return value type is SqlDataReader, then when creating the connection string, we cannot write the following

public static SqlDataReader ExecuteReader (String strSQL)

{

using (SqlConnection connection = new SqlConnection (connectionString))

{

using (SqlCommand cmd = new SqlCommand (strSQL, connection))

{

Try

{

Connection. Open ();

SqlDataReader myreader = cmd. ExecuteReader (commandbehavior.closeconnection);

return myreader;

}

catch (System.Data.SqlClient.SqlException ex)

{

throw new Exception (ex. Message);

}

}

}

}

When you create a use using a, the SqlConnection will be freed when the SqlDataReader is assigned, and the connection will be closed. While we pass the past SqlDataReader is a reference type that receives a place call passing past SqlDataReader, it will prompt that the connection has been closed and cannot be called because the using (SqlConnection connection = new SqlConnection (connectionString)) at the end of the method, the resource is released, and closed the connection, in order to receive the normal delivery of the past SqlDataReader, when creating the connection can not use using, the correct wording as follows

public static SqlDataReader ExecuteReader (String strSQL)

{

SqlConnection connection = new SqlConnection (connectionString);

using (SqlCommand cmd = new SqlCommand (strSQL, connection))

{

Try

{

Connection. Open ();

SqlDataReader myreader = cmd. ExecuteReader (commandbehavior.closeconnection);

return myreader;

}

catch (System.Data.SqlClient.SqlException ex)

{

throw new Exception (ex. Message);

}

}

}

Add Red field: What is the CommandBehavior.CloseConnection effect:

1, the role of CommandBehavior.CloseConnection 2, commandbehavior.closeconnection role 3, Some considerations of commandbehavior.closeconnection in the ExecuteReader method

Return SqlDataReader data in C #/Mssql/winform/asp.net-sqlhelper

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.