SqlConnection状态打开时SqlCommand状态关闭

时间:2015-05-14 01:50:32

标签: c# sql sql-server sqldatareader

我在SqlDataRreader课程中遇到间歇性问题,我在该课程中打开了SqlConnection,其状态为OPEN,但是当我使用SqlCommand创建SqlConnection时{1}},SqlConnection的状态为CLOSED。这仅发生在大约十分之一的尝试中,因此可能是计时问题。

请注意,我不是将连接放在Using块中,而是单独打开/关闭连接,因为我通常一次执行多个命令,但是问题通常发生在第一次在刚刚连接的命令上执行命令时被打开了。

连接代码是:

    private SqlConnection sql;

    public Result Connect(string database)
    {
        string connection = Config.environments[Config.environment][database];
        try
        {
            // Create and open the connection
            sql = new SqlConnection(connection);
            sql.Open();

            if (sql == null || sql.State != System.Data.ConnectionState.Open)
                return new Result(false, "Connect to Database", "Could not connect to database [" + connection + "]");
            return new Result(true, "Connect to Database", "Connected to database [" + connection + "]");
        }
        catch (Exception e)
        {
            return new Result(false, "Connect to Database", "Could not connect to database [" + connection + "] " + e.ToString());
        }
    }

运行命令代码为:

    private DataTable RunSql(string statement)
    {
        if (sql == null || sql.State != System.Data.ConnectionState.Open)
            throw new ScriptException("Cannot execute SQL command, no database connection established [" + statement + "]");

        // Create and execute the SQL statement
        using (SqlCommand command = new SqlCommand(statement, sql))
        {
            command.CommandTimeout = Config.sqlTimeout;

            try
            {
                using (SqlDataReader reader = command.ExecuteReader()) // ERROR OCCURS HERE! - sql.State is OPEN, but command.State is CLOSED ???
                {
                    // Check is the reader has any rresults
                    if (reader.HasRows)
                    {
                        DataTable data = new DataTable();
                        data.Load(reader);
                        return data;
                    }
                    else
                    {
                        throw new Exception("No results found for statement: " + statement + ", on server: " + sql.DataSource + ", in database: " + sql.Database);
                    }
                }
            }
            catch (SqlException)
            {
                //Log things here
            }

            throw new ScriptException("Error executing sql command: " + statement);
        }
    }

重现问题的代码(偶尔):

    private DataTable RunSingleCommand(string database, string command)
    {
        Log(Connect(database));
        return RunSql(command);
    }

1 个答案:

答案 0 :(得分:0)

我可以建议在打开连接之前在Connection命令中,给出条件检查以了解连接当前是否打开。仅当其处于关闭模式时才打开新连接

try
{
    // Create and open the connection
    sql = new SqlConnection(connection);

    if (sql.State != System.Data.ConnectionState.Open)
    {  
        sql.Open();
    }

    return new Result(true, "Connect to Database", "Connected to database [" + connection + "]"); 
}    
catch (Exception e) 
{
    return new Result(false, "Connect to Database", "Could not connect to database [" + connection + "] " + e.ToString());
}