There is already an open and this command is associated with the DataReader, whi

1 the requirements of ExecuteReader is open and available. The current state of connection for connecting.
2 has been opened with this command associated DataReader, which must be closed first.

Frequently report the 2 mistake. Everybody to help me to have a look the code where the problem is it right?. .


 private static SqlConnection connection;
        public static SqlConnection Connection
        {
            get
            {
                string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                if (connection == null)
                {
                    connection = new SqlConnection(connectionString);
                    connection.Open();
                }
                else if (connection.State == System.Data.ConnectionState.Closed)
                {
                    connection.Open();
                }
                else if (connection.State == System.Data.ConnectionState.Broken)
                {
                    connection.Close();
                    connection.Open();
                }
                return connection;
            }
        }

//The other is to call CONNECTION. The following should be no problem
    public static int ExecuteCommand(string safeSql)
        {
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            int result = cmd.ExecuteNonQuery();
            return result;
        }




        public static int ExecuteCommand(string sql, params SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            return cmd.ExecuteNonQuery();
        }

        public static int GetScalar(string safeSql)
        {
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            int result = Convert.ToInt32(cmd.ExecuteScalar());
            return result;
        }

Started by Sandra at November 16, 2016 - 8:25 AM

I use using (SqlConnection conn = new SqlConnection) so that it will be at the end of using automatically closes the database connection.

Posted by Rose at November 26, 2016 - 9:11 AM

Do not share connection, need to use when you new a.
Don't open the connection when the need for early in the morning, open to open, timely and to ensure that the finally {closing}.

Posted by Yehudi at December 06, 2016 - 9:58 AM

Habit is wrong, that could have been open connection. When used in open, close run out immediately

Posted by Lucinda at December 07, 2016 - 10:22 AM

In doing so many connection.Open, ensure that there is only one place open

Posted by Moses at December 16, 2016 - 11:13 AM

Can I have a look and write an example

Posted by Sandra at December 23, 2016 - 12:10 PM

Like the following, do not need to write a static Connection. ADO.net own Connection connection pool, connection created from the connection pool for connection string Connection objects.
public static int ExecuteCommand(string safeSql)
{
using (Connection con = new Connection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
SqlCommand cmd = new SqlCommand(safeSql, con);
int result = cmd.ExecuteNonQuery();
return result;
}
}

Posted by Kim at December 26, 2016 - 12:44 PM