簡體   English   中英

我如何從C#中的mdr.Read()命令僅讀取一條記錄

[英]How Can i Read only one record from mdr.Read() command in c#

x是字符串數組,並且要從數據庫中的表中讀取特定的行,例如:我只希望讀取第二行。

while (mdr.Read())
{
    x = mdr[1].ToString().Split(' ');
    y = 0;
    while (y < x.Length)
    {
        MessageBox.Show(x[y]);
        y++;
    }                        
}

您應該實現一個計數器,該計數器在每次迭代時都遞增,並且僅在該計數器達到所需數量后才執行代碼。 IE瀏覽器:

int i = 0;    
while (mdr.Read())
    {
      if(i == 1) //i is 1 when it's the second row
      {
          x = mdr[1].ToString().Split(' ');
          y = 0;
          while (y < x.Length)
          {
              MessageBox.Show(x[y]);
              y++;
          } 
      }
      i++;                       
    }

還請記住對查詢中的數據進行排序,因為如果未指定這樣做,數據庫並不總是以相同的順序返回數據!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM