如何使用 IDataReader 选择 n 行

发布于 2024-12-12 02:29:06 字数 1244 浏览 0 评论 0原文

[ASP .Net - Microsoft Visual Web Developer 2010]

大家好,

我对这段代码有疑问:

With MenuNavCatDataSource
  Dim xReader As Data.IDataReader = .Select(DataSourceSelectArguments.Empty)

  If xReader.Read Then
    MenuNavCat1.Text = xReader.Item("MenuCategoryName")
    MenuNavCat2.Text = xReader.Item("MenuCategoryName")
    MenuNavCat3.Text = xReader.Item("MenuCategoryName")
    MenuNavCat4.Text = xReader.Item("MenuCategoryName")
    MenuNavCat5.Text = xReader.Item("MenuCategoryName")
  End If
End With

我有 5 个标签,我想从数据库中解析标签的内容。该数据库包含从 1 到 5 排序的菜单。我想将其显示在: -“MenuNavCat1”标签,数据库中订单号为 1 的菜单, - 'MenuNavCat2' 标签、数据库中订单号为 2 的菜单等...

如何在代码中添加 where 语句,就像 'WHERE OrderNo = 1' 等...?

需要你们的帮助。

非常感谢你们。


编辑

这是数据库:

Table MenuNavCategory

 | Column Name  |  Data Type   | 
 |-----------------------------| 
 | MenuNavCatID |  int         | 
 | CategoryName |  varchar(20) |  
 | OrderNumber  |  int         |
 |-----------------------------|

并且有一些值插入到表中。 我想在某个标签上显示“CategoryName”。 例如:

lblCat1 | lblCat2 | lblCat3 | lblCat4

然后,使用 xReader.Read,根据 OrderNumber 将数据库上的值存储到该标签上...

[ASP .Net - Microsoft Visual Web Developer 2010]

Hi all,

I've problem with this code:

With MenuNavCatDataSource
  Dim xReader As Data.IDataReader = .Select(DataSourceSelectArguments.Empty)

  If xReader.Read Then
    MenuNavCat1.Text = xReader.Item("MenuCategoryName")
    MenuNavCat2.Text = xReader.Item("MenuCategoryName")
    MenuNavCat3.Text = xReader.Item("MenuCategoryName")
    MenuNavCat4.Text = xReader.Item("MenuCategoryName")
    MenuNavCat5.Text = xReader.Item("MenuCategoryName")
  End If
End With

I've 5 label and I want to parse the content of the label from the database. The database contains menus ordered from 1 to 5. And I want to show it on:
- 'MenuNavCat1' label, menu with order number 1 on database,
- 'MenuNavCat2' label, menu with order number 2 on database, and so on...

How to add where statement to the code, just like 'WHERE OrderNo = 1', and so on..?

Need your help guys..

Thank you so much.


EDIT

Here is the database:

Table MenuNavCategory

 | Column Name  |  Data Type   | 
 |-----------------------------| 
 | MenuNavCatID |  int         | 
 | CategoryName |  varchar(20) |  
 | OrderNumber  |  int         |
 |-----------------------------|

And there is some value inserted to the table.
I want to show the 'CategoryName' onto some label.
For example:

lblCat1 | lblCat2 | lblCat3 | lblCat4

Then, using that xReader.Read, store the value on the database onto that label based on the OrderNumber...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭⌒浅淡时光〆 2024-12-19 02:29:06

DataReader 一次仅读取一行数据。如果您有 5 个标签,那么数据库中应该有 5 行。然后,使用 DataReader 迭代每一行并分配标签:

With MenuNavCatDataSource
 Dim xReader As Data.IDataReader = .Select(DataSourceSelectArguments.Empty)

 'i is just a counter to keep track of what row we are on.
 Dim i as integer = 1

 If xReader.Read Then
  Select Case i
   Case 1 'First Row from DB.
     MenuNavCat1.Text = xReader.Item("MenuCategoryName")
   Case 2 'Second Row from DB... etc.
    MenuNavCat2.Text = xReader.Item("MenuCategoryName")
   Case 3
    MenuNavCat3.Text = xReader.Item("MenuCategoryName")
   Case 4
    MenuNavCat4.Text = xReader.Item("MenuCategoryName")
   Case 5
    MenuNavCat5.Text = xReader.Item("MenuCategoryName")

   x+=1
 End If
End With

上面只是一个尝试解释 DataReader 如何工作的示例。我会找到另一种方法来实际编码它,因为上面依赖于数据库中的行以正确的顺序排列并且非常难看。

The DataReader only reads one row of data at a time. If you have 5 labels then you should have 5 rows in your database. Then you iterate through each row using the DataReader and assign the labels:

With MenuNavCatDataSource
 Dim xReader As Data.IDataReader = .Select(DataSourceSelectArguments.Empty)

 'i is just a counter to keep track of what row we are on.
 Dim i as integer = 1

 If xReader.Read Then
  Select Case i
   Case 1 'First Row from DB.
     MenuNavCat1.Text = xReader.Item("MenuCategoryName")
   Case 2 'Second Row from DB... etc.
    MenuNavCat2.Text = xReader.Item("MenuCategoryName")
   Case 3
    MenuNavCat3.Text = xReader.Item("MenuCategoryName")
   Case 4
    MenuNavCat4.Text = xReader.Item("MenuCategoryName")
   Case 5
    MenuNavCat5.Text = xReader.Item("MenuCategoryName")

   x+=1
 End If
End With

The above is just an example to try and explain how the DataReader works. I'd find another way to actually code it as the above relies on the rows in the database being in the correct order and is pretty ugly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文