第二次使用 DataReader 错误
DataReader error when i us it in the second time
var filterConditions = new[] {
CreateSqlFilter("CIVILIDD", ID_No, selectCommand, false),
};
string filterCondition = filterConditions.Any(a => a != null) ? filterConditions.Where(a => a != null).Aggregate((filter1, filter2) => String.Format("{0} AND {1}", filter1, filter2)) : (string)null;
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["my"].ConnectionString))
{
selectCommand.Connection = connection;
selectCommand.CommandText = filterCondition == null ? "SELECT * FROM _4" : "SELECT * FROM _4 WHERE " + filterCondition;
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
DataTable dataSource = new DataTable();
adapter.Fill(dataSource);
dataGridView2.DataSource = dataSource;
using (SqlCommand command2 = new SqlCommand("SELECT * FROM [_4] WHERE CIVILIDD = @id", mycon))
{
command2.Parameters.AddWithValue("@id", ID_No.Text);
SqlDataReader dr = command2.ExecuteReader();
while (dr.Read())
{
txtname1.Text = (dr["name1"].ToString());
txtname2.Text = (dr["name2"].ToString());
Governorate.Text = (dr["Governorate"].ToString());
City.Text = (dr["City"].ToString());
Block.Text = (dr["Block"].ToString());
Street.Text = (dr["Street"].ToString());
Avenue.Text = (dr["Avenue"].ToString());
House.Text = (dr["House"].ToString());
Floor.Text = (dr["Floor"].ToString());
flat.Text = (dr["flat"].ToString());
}
}
}
我使用这段代码来过滤我的数据库,当我第一次 运行 时我得到了那个错误,它在第二次工作时很好我得到了这个错误
System.InvalidOperationException: 'There is already an open DataReader associated with this Command which must be closed first.'
in this line SqlDataReader dr = command2.ExecuteReader();
您必须处理适配器创建的第一个数据reader。 using 语句将处理开箱即用的资源。
您只能将数据 reader 与一个命令相关联(就像错误状态一样)。创建2个using语句,准备2个datareaders。这可以在同一个 using( connection ) 中,因为一个连接可以用于多个命令,但是每个命令必须有单独的数据readers。
using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM EmployeeIDs", c))
{
}
using( ..command2.. )
{
SqlDataReader dr = command2.ExecuteReader();
}
在那里阅读更多信息 HERE
使用后需要关闭datareadre。
当您不使用 Using()
时,您的代码应该是这样的
看评论看懂
var filterConditions = new[] {
CreateSqlFilter("CIVILIDD", ID_No, selectCommand, false),
};
string filterCondition = filterConditions.Any(a => a != null) ? filterConditions.Where(a => a != null).Aggregate((filter1, filter2) => String.Format("{0} AND {1}", filter1, filter2)) : (string)null;
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["my"].ConnectionString))
{
selectCommand.Connection = connection;
selectCommand.CommandText = filterCondition == null ? "SELECT * FROM _4" : "SELECT * FROM _4 WHERE " + filterCondition;
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
DataTable dataSource = new DataTable();
adapter.Fill(dataSource);
dataGridView2.DataSource = dataSource;
using (SqlCommand command2 = new SqlCommand("SELECT * FROM [_4] WHERE CIVILIDD = @id", mycon))
{
command2.Parameters.AddWithValue("@id", ID_No.Text);
SqlDataReader dr = command2.ExecuteReader();
while (dr.Read())
{
txtname1.Text = (dr["name1"].ToString());
txtname2.Text = (dr["name2"].ToString());
Governorate.Text = (dr["Governorate"].ToString());
City.Text = (dr["City"].ToString());
Block.Text = (dr["Block"].ToString());
Street.Text = (dr["Street"].ToString());
Avenue.Text = (dr["Avenue"].ToString());
House.Text = (dr["House"].ToString());
Floor.Text = (dr["Floor"].ToString());
flat.Text = (dr["flat"].ToString());
}
// Close and Dispose the datareader
dr.Close();
dr.Dispose();
}
}
var filterConditions = new[] {
CreateSqlFilter("CIVILIDD", ID_No, selectCommand, false),
};
string filterCondition = filterConditions.Any(a => a != null) ? filterConditions.Where(a => a != null).Aggregate((filter1, filter2) => String.Format("{0} AND {1}", filter1, filter2)) : (string)null;
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["my"].ConnectionString))
{
selectCommand.Connection = connection;
selectCommand.CommandText = filterCondition == null ? "SELECT * FROM _4" : "SELECT * FROM _4 WHERE " + filterCondition;
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
DataTable dataSource = new DataTable();
adapter.Fill(dataSource);
dataGridView2.DataSource = dataSource;
using (SqlCommand command2 = new SqlCommand("SELECT * FROM [_4] WHERE CIVILIDD = @id", mycon))
{
command2.Parameters.AddWithValue("@id", ID_No.Text);
SqlDataReader dr = command2.ExecuteReader();
while (dr.Read())
{
txtname1.Text = (dr["name1"].ToString());
txtname2.Text = (dr["name2"].ToString());
Governorate.Text = (dr["Governorate"].ToString());
City.Text = (dr["City"].ToString());
Block.Text = (dr["Block"].ToString());
Street.Text = (dr["Street"].ToString());
Avenue.Text = (dr["Avenue"].ToString());
House.Text = (dr["House"].ToString());
Floor.Text = (dr["Floor"].ToString());
flat.Text = (dr["flat"].ToString());
}
}
}
我使用这段代码来过滤我的数据库,当我第一次 运行 时我得到了那个错误,它在第二次工作时很好我得到了这个错误
System.InvalidOperationException: 'There is already an open DataReader associated with this Command which must be closed first.'
in this line SqlDataReader dr = command2.ExecuteReader();
您必须处理适配器创建的第一个数据reader。 using 语句将处理开箱即用的资源。
您只能将数据 reader 与一个命令相关联(就像错误状态一样)。创建2个using语句,准备2个datareaders。这可以在同一个 using( connection ) 中,因为一个连接可以用于多个命令,但是每个命令必须有单独的数据readers。
using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM EmployeeIDs", c))
{
}
using( ..command2.. )
{
SqlDataReader dr = command2.ExecuteReader();
}
在那里阅读更多信息 HERE
使用后需要关闭datareadre。
当您不使用 Using()
看评论看懂
var filterConditions = new[] {
CreateSqlFilter("CIVILIDD", ID_No, selectCommand, false),
};
string filterCondition = filterConditions.Any(a => a != null) ? filterConditions.Where(a => a != null).Aggregate((filter1, filter2) => String.Format("{0} AND {1}", filter1, filter2)) : (string)null;
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["my"].ConnectionString))
{
selectCommand.Connection = connection;
selectCommand.CommandText = filterCondition == null ? "SELECT * FROM _4" : "SELECT * FROM _4 WHERE " + filterCondition;
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
DataTable dataSource = new DataTable();
adapter.Fill(dataSource);
dataGridView2.DataSource = dataSource;
using (SqlCommand command2 = new SqlCommand("SELECT * FROM [_4] WHERE CIVILIDD = @id", mycon))
{
command2.Parameters.AddWithValue("@id", ID_No.Text);
SqlDataReader dr = command2.ExecuteReader();
while (dr.Read())
{
txtname1.Text = (dr["name1"].ToString());
txtname2.Text = (dr["name2"].ToString());
Governorate.Text = (dr["Governorate"].ToString());
City.Text = (dr["City"].ToString());
Block.Text = (dr["Block"].ToString());
Street.Text = (dr["Street"].ToString());
Avenue.Text = (dr["Avenue"].ToString());
House.Text = (dr["House"].ToString());
Floor.Text = (dr["Floor"].ToString());
flat.Text = (dr["flat"].ToString());
}
// Close and Dispose the datareader
dr.Close();
dr.Dispose();
}
}