如果传入的是connection string(连接字符串,而非连接对象),是否有连接未释放关闭的问题?-------------
不会啊,如果你传入的是connectionstring的话:在方法的内部是用
using(SqlConnection conn = new SqlConnection(connectionstring))
{
}
来实现的,使用using语句意味着在using语句结束时调用 conn.Dispose

解决方案 »

  1.   

    using语句意味着在using语句结束时  Dispose
      

  2.   

    using语句只能用于实现了IDispose接口的对象
    相当于一个try{}finally{}块
    保证了出现异常也能Dispose
      

  3.   

    public static SqlDataReader ExecuteReader(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) {
    SqlCommand cmd = new SqlCommand();
    SqlConnection conn = new SqlConnection(connString); // we use a try/catch here because if the method throws an exception we want to 
    // close the connection throw code, because no datareader will exist, hence the 
    // commandBehaviour.CloseConnection will not work
    try {
    PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
    SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    cmd.Parameters.Clear();
    return rdr;
    }catch {
    conn.Close();
    throw;
    }
    }

    /// <summary>
    /// Execute a SqlCommand that returns the first column of the first record against the database specified in the connection string 
    /// using the provided parameters.
    /// </summary>
    /// <res>
    /// e.g.:  
    ///  Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
    /// </res>
    /// <param name="connectionString">a valid connection string for a SqlConnection</param>
    /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
    /// <param name="commandText">the stored procedure name or T-SQL command</param>
    /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
    /// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>
    public static object ExecuteScalar(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) {
    SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connString)) {
    PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
    object val = cmd.ExecuteScalar();
    cmd.Parameters.Clear();
    return val;
    }
    说明CONNECTION肯定被关闭呢
      

  4.   

    众位说得在理但
    using (SqlConnection conn = new SqlConnection(connString)) {...}
    SqlHelper的源代码没有这样写啊!
    没有发现using 语句块,这就是我的疑问啊!
      

  5.   

    我也看了SqlHelper所带的例子,它调用时传的是显式SqlConnection对象,而非ConnectionString
    调用完毕后显式关闭并销毁连接。
    我现在确实无法确定传入连接字符串是否有问题啊?对其熟悉的人,再来好好探讨一下啊。
      

  6.   

    public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    {
    if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );// Create & open a SqlConnection, and dispose of it after we are done
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
    connection.Open();// Call the overload that takes a connection in place of the connection string
    return ExecuteNonQuery(connection, commandType, commandText, commandParameters);
    }
    }怎么不是这么写的...
      

  7.   

    没有使用using时:public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
    {
    if( connection == null ) throw new ArgumentNullException( "connection" );// Create a command and prepare it for execution
    SqlCommand cmd = new SqlCommand();
    bool mustCloseConnection = false;
    PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection );
        
    // Finally, execute the command
    int retval = cmd.ExecuteNonQuery();
        
    // Detach the SqlParameters from the command object, so they can be used again
    cmd.Parameters.Clear();
    if( mustCloseConnection )
    connection.Close();
    return retval;
    }
      

  8.   

    Sunmast(速马)你看的比我仔细
    没错,你说得有理,我有点粗心。多谢了。另外再问,再SqlHelper的基础上,你是否开发有好的类库!