<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