阅读器关闭时读取的次数无效 - 但是没有关闭阅读器?

时间:2013-03-22 11:12:46

标签: c# sql sql-server-ce

在我的C#应用​​程序中,我正在与SQL Compact数据库连接。在这方面,我有三张桌子; customerlistcustomerlist(客户和列表是多对多关系)。

当我想要删除列表时,我有一个函数。该函数也会删除customerlist表中的相关条目,仅用于清洁(因为如果列表本身已被删除,它们将不存在)。

我的代码就是这样:

    private void clearRedundantSubscriptions()
    {
        string sql;
        // Check if there are any entries in customerlist table which point to non-existing lists
        try
        {
            sql = "select distinct cl.listid from customerlist cl inner join list l on cl.listid != l.listid";
            SqlCeCommand cmdGetDisusedLists = new SqlCeCommand(sql, DbConnection.ceConnection);
            SqlCeDataReader reader = cmdGetDisusedLists.ExecuteReader();
            while (reader.Read())
            {
                DbFunctions.DeleteList(reader.GetInt32(0), false, false);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error cleaning up list entries." + ex.Message);
        }
        return;
    }

    public static bool DeleteList(int id, bool display, bool close)
    {
        string sql;
        string title = "";
        bool ranOk = false;
        try
        {
            sql = "select ShortDesc from list where listid=" + id;
            DbFunctions.runSQL(sql, out title);
            sql = "delete from list where ListId=" + id;
            SqlCeCommand cmdDelList = new SqlCeCommand(sql, DbConnection.ceConnection);
            cmdDelList.ExecuteNonQuery();
            sql = "delete from customerlist where listid=" + id;
            SqlCeCommand cmdDelEntries = new SqlCeCommand(sql, DbConnection.ceConnection);
            cmdDelEntries.ExecuteNonQuery();
            if (display)
                General.doneWork(title + " list deleted.");
            ranOk = true;
        }
        catch (Exception ex)
        {
            if (display)
                MessageBox.Show("Unable to delete list. " + ex.Message);
        }
        finally
        {
            if (close)
                DbConnection.closeConnection();
        }
        return ranOk;
    }

    public static void closeConnection()
    {
        if (_sqlCeConnection != null)
            _sqlCeConnection.Close();
    }

你会注意到我的删除函数,我传入一个名为'close'的bool参数。我添加了这个,因为在阅读器内部关闭与数据库的连接导致上述错误。所以现在,如果我想在阅读器中使用这个函数,我调用deleteList函数并确保'close'参数作为false传入。这不是问题 - 这意味着在此函数中调用了DbConnection.closeConnection NOT

所以我没有关闭阅读器,也没有关闭数据库连接。所以任何想法为什么我仍然会收到此错误?

1 个答案:

答案 0 :(得分:1)

我修改了你的代码,试试这个。

我不知道你的DbFunctions.runSQL方法中发生了什么,所以你可能需要在那次调用之前重新打开连接。

private void clearRedundantSubscriptions()
{
    string sql;
    // Check if there are any entries in customerlist table which point to non-existing lists
    var list = new List<int>();
    try
    {
        if (DbConnection.ceConnection.State == ConnectionState.Closed)
            DbConnection.ceConnection.Open();

        sql = "select distinct cl.listid from customerlist cl inner join list l on cl.listid != l.listid";

        SqlCeCommand cmdGetDisusedLists = new SqlCeCommand(sql, DbConnection.ceConnection);
        SqlCeDataReader reader = cmdGetDisusedLists.ExecuteReader();
        while (reader.Read())
        {
            list.Add(reader.GetInt32(0));
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error cleaning up list entries." + ex.Message);
        throw;
    }
    finally
    {
        DbConnection.closeConnection();
    }

    foreach(var id in list)
    {
        DeleteList(id,false);

    }
    return;
}

public static bool DeleteList(int id, bool display)
{
    string sql;
    string title = "";
    bool ranOk = false;
    try
    {
        sql = "select ShortDesc from list where listid=" + id;
        DbFunctions.runSQL(sql, out title);


        sql = "delete from list where ListId=" + id;
        SqlCeCommand cmdDelList = new SqlCeCommand(sql, DbConnection.ceConnection);
        cmdDelList.ExecuteNonQuery();
        sql = "delete from customerlist where listid=" + id;
        SqlCeCommand cmdDelEntries = new SqlCeCommand(sql, DbConnection.ceConnection);
        cmdDelEntries.ExecuteNonQuery();
        if (display)
            General.doneWork(title + " list deleted.");
        ranOk = true;
    }
    catch (Exception ex)
    {
        if (display)
            MessageBox.Show("Unable to delete list. " + ex.Message);
    }
    finally
    {
            DbConnection.closeConnection();
    }
    return ranOk;
}

public static void closeConnection()
{
    if (_sqlCeConnection != null)
        _sqlCeConnection.Close();
}
相关问题