从数据库访问数据时执行while循环 [英] do while loop while accessing data from database

查看:103
本文介绍了从数据库访问数据时执行while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以在两个嵌套的while循环中使用两个不同的读取器吗?

代码在下面,并且我使用c#.net和ms访问权限

can we two different readers in two nested while loops

the code is below and m use c#.net and ms access

string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ui.mdb";
          OleDbConnection connst = new OleDbConnection(conn);
          connst.Open();
          string str = "select * from Tabs ";




          OleDbCommand cmd = new OleDbCommand(str, connst);

          OleDbDataReader DataReader = cmd.ExecuteReader();
     while (DataReader.Read())
          {

              TabID = Convert.ToInt32(DataReader[0]);
              TabName = DataReader[1].ToString();
              TabCaption = DataReader[2].ToString();
              TabHome = RibbonBar.InsertTab(TabID, TabCaption);
              TabHome.Id = TabID;

              OleDbDataReader ReadGroup;
              do
              {
                  string str1 = "select * from Groups where Tab_ID=" + TabID;
                  OleDbCommand CmdGroup = new OleDbCommand(str1, connst);

                  ReadGroup = CmdGroup.ExecuteReader();
                  ReadGroup.Read();
                  Console.WriteLine(ReadGroup.HasRows);
              }


              while (ReadGroup.Read());
              {
                  GroupID = Convert.ToInt32(ReadGroup[0]);
                  GroupName = ReadGroup[1].ToString();
                  GroupCaption = ReadGroup[2].ToString();
                  Tests = TabHome.Groups.AddGroup(GroupCaption, GroupID);

                  //for (int k = j; k <= i; k++)
                  //{
                  string str2 = "select * from Items where Group_id=0";
                  OleDbCommand CmdControls = new OleDbCommand(str2, connst);
                  OleDbDataReader ReadControl = cmd.ExecuteReader();
                  while (ReadControl.Read())
                  {
                      ControlID = Convert.ToInt32(ReadControl[0]);
                      ControlName = ReadControl[1].ToString();
                      XtremeCommandBars.XTPControlType typ = (XtremeCommandBars.XTPControlType)ControlID;
                      Tests.Add(typ, ControlID, ControlName, false, false);
                      Console.WriteLine(XtremeCommandBars.XTPControlType.xtpControlButton.GetTypeCode());
                      int lm;
                      lm = Convert.ToInt32(typ.GetTypeCode());

                  }
              }
                //}
          }

推荐答案

确定-淘汰自己.

如果您需要更详细的答案,请发布代码,告诉我们您使用的是哪种语言(是否是VBScript?),并说明您要尝试执行的操作以及错误的出处.
Sure - knock yourself out.

If you want a more detailed answer, post your code, tell us what language you''re using ( is it VBScript or not ? ), and explain what you''re trying to do, and where it''s going wrong.


请正确设置代码格式;使用代码块"按钮,并确保整个代码块在< pre></pre>标签.执行完快速预览"标签后,您可以对其进行检查.
Please format your code properly; use the "code block" button and make sure the entire code block is within the <pre></pre> tags. You can check it after you do this with the "Quick Preview" tab.


我看不出有任何不起作用的原因.我认为您认为它不起作用,所以也许您需要告诉我们什么不起作用,您希望它做什么,您通过调试代码学到了什么,等等.

大括号围绕GroupID = Convert.ToInt32(ReadGroup [0]);直到ReadControl.Read()循环,使代码难以阅读,因为您在它们上方有一个while语句,并且需要一秒钟的时间才能意识到将它附加到一个do上.括号什么也没有增加,并且损害了可读性,我会丢掉它们.
I don''t see any reason for this not to work. I assume you think it''s not working, so perhaps you need to tell us what doesn''t work, what you expect it to do, what you''ve learned through debugging the code, etc.

The braces around GroupID = Convert.ToInt32(ReadGroup[0]); down to the ReadControl.Read() loop, make the code hard to read, because you have a while statment above them and it takes a second to realise it''s attached to a do. The braces add nothing and hurt readability, I''d lose them.


这篇关于从数据库访问数据时执行while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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