c# - 我没有关闭以前的 DataReader,但是在哪里?

标签 c# exception ms-access oledb

我已经更改了我以前的代码,所以我没有使用“使用”。它工作得更早,不同类中的代码基本上代表同一个东西,但它的工作。

我已经盯着它看了 2 个小时了,我只是想不出问题出在哪里。

我只有一个阅读器,但每次我使用 DisplayFileContent 方法时都会收到错误:错误:已经有一个打开的 DataReader 与此命令关联,必须先将其关闭。

// May be public so we can display
// content of file from different forms.
public void DisplayFileContent(string filePath)
{
    // Counting all entries.
    int countEntries = 0;

    // Encrypting/Decrypting  data.
    EncryptDecrypt security = new EncryptDecrypt();

    using (OleDbConnection connection = new OleDbConnection())
    {
        connection.ConnectionString =
            "Provider=Microsoft.ACE.OLEDB.12.0;" +
            "Data Source=" + filePath + ";" +
            "Persist Security Info=False;" +
            "Jet OLEDB:Database Password=" + 
            hashPhrase.ShortHash(storedAuth.Password) + ";";

        using (OleDbCommand command = new OleDbCommand
            ("Select * FROM PersonalData", connection))
        {
            OleDbDataReader read;

            try
            {
                // Open database connection.
                connection.Open();

                // Create a data reader.
                read = command.ExecuteReader();

                // Clearing the textbox before proceeding.
                txtDisplay.Text = string.Empty;

                // Checking if there is any data in the file.
                if (read.HasRows)
                {
                    // Reading information from the file.
                    while (read.Read())
                    {
                        // Count all entries read from the reader.
                        countEntries++;

                        // Reading all values from the file as string.
                        // While each string is encrypted, we must decrypt them.
                        // User name and password is the same as user provided
                        // while authentication.
                        txtDisplay.Text += "=== Entry ID: " + read.GetValue(0) +
                            " ===" + Environment.NewLine;
                        txtDisplay.Text += "Type: " + security.Decrypt
                            (read.GetString(1), storedAuth.Password,
                            storedAuth.UserName) + Environment.NewLine;
                        if (!read.IsDBNull(2))
                            txtDisplay.Text += "URL: " +
                                security.Decrypt(read.GetString(2),
                                storedAuth.Password, storedAuth.UserName) +
                                Environment.NewLine;
                        if (!read.IsDBNull(3))
                            txtDisplay.Text += "Software Name: " +
                                security.Decrypt(read.GetString(3),
                                storedAuth.Password, storedAuth.UserName) +
                                Environment.NewLine;
                        if (!read.IsDBNull(4))
                            txtDisplay.Text += "Serial Code: " +
                                security.Decrypt(read.GetString(4),
                                storedAuth.Password, storedAuth.UserName) +
                                Environment.NewLine;
                        if (!read.IsDBNull(5))
                            txtDisplay.Text += "User Name: " +
                                security.Decrypt(read.GetString(5),
                                storedAuth.Password, storedAuth.UserName) +
                                Environment.NewLine;
                        if (!read.IsDBNull(6))
                            txtDisplay.Text += "Password: " +
                                security.Decrypt(read.GetString(6),
                                storedAuth.Password, storedAuth.UserName) +
                                Environment.NewLine;
                        txtDisplay.Text += Environment.NewLine;
                    }
                }
                else
                {
                    txtDisplay.Text = "There is nothing to display! " +
                        "You must add something before so I can display anything here.";
                }

                // Displaying number of entries in the status bar.
                tsslStatus.Text = "A total of " + countEntries + " entries.";

                // Selecting 0 character to make sure text
                // isn't completly selected.
                txtDisplay.SelectionStart = 0;

                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }
    }
}

最佳答案

您正在 catch block 之前调用 command.ExecuteNonQuery。您需要先关闭 DataReader。

无论如何,我建议将使用数据读取器的代码包装在一个 using block 中:

using(OleDbDatareader read = command.ExecuteReader())
{

   ...

}

如上所述,command.ExecuteNonQuery() 用于执行您不希望从中返回结果的命令。这些通常是插入、更新或删除,但也可能包括执行相同操作的存储过程调用,或者您不关心返回结果的地方

关于c# - 我没有关闭以前的 DataReader,但是在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8350036/

相关文章:

c++ - 嵌入式 C++ : to use exceptions or not?

vba - 限制用户对 MS Access 中的表和查询的 Access

sql - 检查表中存在的列

c# - 如何使用 ASP.NET C# 将 Excel 数据导入到 SQL Server,其中表的数据不从第一行开始?

c# - 启用 bool 值并在 View 中输入文本,然后传回 Controller - MVC

c# - Nido的ASP.NET C#异常处理

c# - Access sql 以使用自动编号创建表

c# - 检查线程是否在 Gui 上下文中

java - 当 Constructor#newInstance 抛出异常时获取实例?

c# - 序列不包含匹配元素