本文介绍了数据读取器已经打开问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好... !!!!!!
我正在通过datareader检查数据库中存在的值,如果该值存在,那么我会将该表中的某些其他值绑定到datagrid.
问题是,当它在数据库中找到值并输入条件时,它说datareader已经打开;如果我关闭了datareader,则条件将仅被第一次检查一次,然后连接将被终止.
Hi all....!!!
i am checking values that exist in database through datareader and if the value exists then i am binding some other values from that table to datagrid.
The problem is that when it finds the value in database and enters if condition it says datareader already open and if i close the datareader then condition will be checked only once for the first time and then the connection will be terminated.
con.Open();
SqlCommand cmd = new SqlCommand("Select Productcode, Status from Product", con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
string code = dr.GetValue(0).ToString();
string status = dr.GetValue(1).ToString();
if (status == "Active")
{
// dr.Close();
SqlCommand cmd1 = new SqlCommand("Select Productcode, Name, MRP from Product where Productcode=''" + code + "''", con);
da = new SqlDataAdapter(cmd1);
ds = new DataSet();
da.Fill(ds);
grid_pins.DataSource = ds;
grid_pins.DataBind();
}
}
推荐答案
//Declaration and initialization of con here.
con.Open();
SqlCommand cmd1 = new SqlCommand("Select Productcode, Name, MRP from Product where Status='Active'", con);
cmd1.CommandType = CommandType.Text;
da = new SqlDataAdapter(cmd1);
ds = new DataSet();
da.Fill(ds);
grid_pins.DataSource = ds;
grid_pins.DataBind();
SqlConnection con = new SqlConnection(con);
SqlCommand cmd = new SqlCommand("Select Productcode, Name, MRP from Product where Status='Active'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
grid_pins.DataSource = ds;
grid_pins.DataBind();
这篇关于数据读取器已经打开问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!