数据读取器已经打开问题 [英] Data reader already open problem

查看:99
本文介绍了数据读取器已经打开问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好... !!!!!!

我正在通过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();
            }
        }

推荐答案

您的代码存在多个问题.

1)DataReader处于打开模式时,无法执行SQLCommand.

2)我猜你想从Product表获得的记录具有Status与活动"匹配的内容.

3)尝试如下更改代码.
Multiple issues with your code.

1) You can not execute SQLCommand when your DataReader is in open mode.

2) I guess you want records from Product Table which are having Status matching with ''Active''.

3) Try by changing your code as below.
//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();


Why u use two queries..Use Single Query..

Why u use two queries..Use Single Query..

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();


安装了将SQL阅读器用于

SqlCommand cmd =新的SqlCommand(从产品中选择产品代码,状态",con);

使用DataTable并使用
关闭您的连接
cmd.Connection.close()

在关闭Connection之前将结果存储在datatable中,并使用此datatable

foreach(datatable.Rows中的DataRow dr)
{
//您的第二个查询
//即SqlCommand cmd1 = new SqlCommand(从产品中选择产品代码,名称,MRP,其中Status =" Active",con);

}
insted of using sql reader for

SqlCommand cmd = new SqlCommand("Select Productcode, Status from Product", con);

use DataTable and close you connection using

cmd.Connection.close()

store result in datatable before closing Connection and use this datatable

foreach(DataRow dr in datatable.Rows)
{
// your 2nd query
// i.e. SqlCommand cmd1 = new SqlCommand("Select Productcode, Name, MRP from Product where Status=''Active''", con);

}


这篇关于数据读取器已经打开问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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