asp.net中的sqldatareader [英] sqldatareader in asp.net

查看:102
本文介绍了asp.net中的sqldatareader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
如果我使用此代码,则显示错误,例如已经有与此命令关联的打开的数据读取器.
请在关闭此数据读取器的地方帮帮我.

Hi All,
if i used this code then it shown error like there is already an open datareader associated with this command.
please help me where i closed this datareader.

SqlConnection con = Database.GetConnection();
        SqlCommand com = new SqlCommand("SELECT * FROM category WHERE article_allow = 1", con);
        string article_visit = "0";
        SqlDataReader rs = com.ExecuteReader();
        while (rs.Read())
        {
            SqlCommand cmd = new SqlCommand("SELECT article_visit FROM article_list WHERE cat_ID = @catID AND article_status=1", con);
            cmd.Parameters.AddWithValue("catID", rs["cat_ID"]);
            SqlDataReader rst = cmd.ExecuteReader();
            while (rst.Read())
            {
                article_visit = rst["article_visit"] + article_visit;
            }
            rst.Close();
            rst.Dispose();
            cmd.Dispose();
        }
        rs.Close();
        rs.Dispose();


请帮助解决该问题.
在此先感谢


please help how to solve this.
Thanks in advance

推荐答案

这是C#,语法必须仅是C#.使用方括号和"+"代替:
SqlCommand cmd = new SqlCommand("SELECT article_visit FROM article_list where cat_ID=" + rs["cat_ID"] + " and article_status=1", con);
代替
SqlCommand cmd = new SqlCommand("SELECT article_visit FROM article_list where cat_ID=" & rs("cat_ID") & " and article_status=1", con);


article_visit = rst["article_visit"] + article_visit;
代替
article_visit = rst("article_visit") + article_visit;

我相信您已经从某处复制了它!
This is C# and the syntax has to be that of C# only. Use square brackets and ''+'' instead:
SqlCommand cmd = new SqlCommand("SELECT article_visit FROM article_list where cat_ID=" + rs["cat_ID"] + " and article_status=1", con);
in place of
SqlCommand cmd = new SqlCommand("SELECT article_visit FROM article_list where cat_ID=" & rs("cat_ID") & " and article_status=1", con);

and
article_visit = rst["article_visit"] + article_visit;
in place of
article_visit = rst("article_visit") + article_visit;

I believe you have copied it from somewhere!


将代码替换为...

Replace your code with...

SqlConnection con = Database.GetConnection();
        SqlCommand com = new SqlCommand("select * from category where article_allow = 1", con);
        SqlDataReader rs = new SqlDataReader();
        SqlDataReader rst = new SqlDataReader();
        
        string article_visit = "0";
        rs = com.ExecuteReader();
        while (rs.Read())
        {
           
            SqlCommand cmd = new SqlCommand("SELECT article_visit FROM article_list where cat_ID=" + rs["cat_ID"] + " and article_status=1", con);
            rst = cmd.ExecuteReader();
            while (rst.Read())
            {
               
                article_visit = rst["article_visit"] + article_visit;
            }
            
            rst.Close();
 
            
            cmd.Clone();
        }
        rs.Close();




现在,您的代码中出现了什么问题:请注意,rs("cat_ID")是一个方法调用,而它应该类似于rs ["cat_ID"].同样,对于rst("article_visit")是rst ["article_visit"].也是用&符号代替&

希望您明白了.......:)




Now whats the problem in your code : Notice that rs("cat_ID") is a method call, while it should be like this rs["cat_ID"]. Similarly for rst("article_visit") is rst["article_visit"]. also the + sign in place of &

Hope you got the point ....... :)


如何创建SqlDataReader的 NEW 实例?
不要创建SQLDatareader的 new 实例,这会给您compile time错误.

可能是您运行的旧版本代码.
How you create NEW instance of SqlDataReader ?
Do not create new instance of SQLDatareader, it will give you complie time error.

may be your older version of code running.


这篇关于asp.net中的sqldatareader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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