我没有关闭之前的DataReader,但是在哪里呢?
我已经更改了以前的代码,因此我没有使用“使用”。它工作得更早,不同类中的代码基本上代表相同的东西,但它的工作原理。
我已经盯着它看了两个小时了,我就是不知道问题出在哪里。
我只有一个阅读器,但每次使用 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);
}
}
}
}
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);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在 catch 块之前调用 command.ExecuteNonQuery 。您需要先关闭 DataReader。
无论如何,我建议将使用数据读取器的代码包装在 using 块中:
如上所述,command.ExecuteNonQuery() 用于执行您不期望返回结果的命令。这些通常是插入、更新或删除,但也可能包括执行相同操作的存储过程调用,或者您不关心返回结果的调用
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:
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
在您上线之前,
您需要:
此外,重要的是要知道使用连接不会自动关闭它。因此,在使用 using 语句结束连接之前,您需要一个
Before your line
you need to:
In addition, it is critical to know that using a connection does NOT automatically close it. Therefore, before the end of your connection using statement, you need a
我没有看到关闭数据读取器的代码。
在catch部分之后添加finally:
编辑#1:我犯了一个错误,您应该在执行下一个命令之前将其关闭,因此要么删除catch块之前的最后一行(如果合适)并添加finally块,要么只是添加使用来读取变量。
I see no code to close data reader.
Add finally after catch part:
EDIT#1: I made a mistake, You should close it before executing next command, so either delete last line (if appropriate) before catch block and add finally block or just add using to read variable.