我没有关闭以前的DataReader,但在哪里? [英] I didn't close previous DataReader, but where?

查看:93
本文介绍了我没有关闭以前的DataReader,但在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我,所以我没有使用'使用'改变了我以前的代码。早前工作,并在不同的类代码基本上是同样的东西,但它的工作。



我它已经盯着2小时,现在,我只是想不通。哪里出了问题,可能是



我只有一个读者,但我使用DisplayFileContent方法,我得到的错误每次:错误:有已经使用此命令,必须先关闭相关联的打开的DataReader。

  //可能市民这样我们就可以显示
//文件的不同形式的内容。
公共无效DisplayFileContent(字符串文件路径)
{
//计数所有条目。
INT countEntries = 0;

//加密/解密数据。
EncryptDecrypt安全=新EncryptDecrypt();使用(OleDbConnection的连接=新的OleDbConnection())
{
connection.ConnectionString =


供应商= Microsoft.ACE.OLEDB.12.0; +
数据源=+文件路径+; +
持续安全信息=假; +
的Jet OLEDB:数据库密码=+
hashPhrase.ShortHash(storedAuth.Password)+;;使用

(OleDbCommand的命令=新的OleDbCommand
(SELECT * FROM PersonalData连接))
{
OleDbDataReader读取;


{
//打开数据库连接。
connection.Open();

//创建一个数据读取器。
读= Command.ExecuteReader却();

//继续之前清除文本框。
txtDisplay.Text =的String.Empty;

//检查是否存在的文件中的任何数据。
如果(read.HasRows)
{
//从文件读取信息。
而(read.Read())
{
//计数从阅读器读取的所有条目。
countEntries ++;

//从文件中读取字符串的所有值。
//虽然每个字符串加密,我们必须解密。
//用户名和密码是一样的用户提供的
//同时验证。
txtDisplay.Text + ====条目ID:+ read.GetValue(0)+
===+ Environment.NewLine;
txtDisplay.Text + =类型:+ security.Decrypt
(read.GetString(1),storedAuth.Password,
storedAuth.UserName)+ Environment.NewLine; (!read.IsDBNull(2))
如果
txtDisplay.Text + =URL:+
security.Decrypt(read.GetString(2),
storedAuth.Password ,storedAuth.UserName)+
Environment.NewLine;
如果
txtDisplay.Text + =软件名称:(read.IsDBNull(3)!)+
security.Decrypt(read.GetString(3),
storedAuth。密码,storedAuth.UserName)+
Environment.NewLine;
如果
txtDisplay.Text + =串行代码:(read.IsDBNull(4)!)+
security.Decrypt(read.GetString(4),
storedAuth。密码,storedAuth.UserName)+
Environment.NewLine;
如果
txtDisplay.Text + =用户名:(read.IsDBNull(5)!)+
security.Decrypt(read.GetString(5),
storedAuth。密码,storedAuth.UserName)+
Environment.NewLine; (!read.IsDBNull(6))
如果
txtDisplay.Text + =密码:+
security.Decrypt(read.GetString(6),
storedAuth.Password ,storedAuth.UserName)+
Environment.NewLine;
txtDisplay.Text + = Environment.NewLine;
}
}
,否则
{
txtDisplay.Text =没有什么可显示!+
:你必须之前,所以我可以添加的东西这里显示任何内容。;
}

//显示在状态栏条目数。
tsslStatus.Text =共有+ countEntries +的条目。

//选择0字符以确保文本
//没有完全地选择。
txtDisplay.SelectionStart = 0;

command.ExecuteNonQuery();
}
赶上(异常前)
{
MessageBox.Show(错误:+ ex.Message);
}
}
}
}


解决方案

您只是catch块之前调用command.ExecuteNonQuery。你需要先关闭您的DataReader



我建议包装,利用在使用块DataReader的反正代码:

 使用(OleDbDatareader读= Command.ExecuteReader却())
{



}

正如上面所指出的,command.ExecuteNonQuery()是执行命令,你不期望从返回结果。这些通常是插入,更新或删除,但也可能包括存储过程调用,这样做,或者你不关心返回的结果。


I have changed my previous code so I am not using 'using'. It work earlier, and code in different class basically represent the same thing, but its working.

I have stared at it for 2 hours now and I just can't figure out where the problems might be.

I have only one reader but each time I am using DisplayFileContent method I am getting the error: Error: There is already an open DataReader associated with this command which must be closed first.

// 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);
            }
        }
    }
}

解决方案

You are calling command.ExecuteNonQuery just before the catch block. You'll need to close your DataReader first.

I'd recommend wrapping the code that utilizes the datareader in a using block anyway:

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

   ...

}

As pointed out above, command.ExecuteNonQuery() is for executing commands that you don't expect a return result from. These typically are inserts, updates or deletes, but may also include stored proc calls that do the same, or where you don't care about the returned result

这篇关于我没有关闭以前的DataReader,但在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆