2013-07-25 80 views
1

我有这个功能从数据库中显示记录到FlowLayoutPanel的极限记录

flowLayoutPanel1.Controls.Clear(); 
     using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString)) 
     { 
      myDatabaseConnection.Open(); 
      string a = "Select EmpID, Name from EmpTable"; 
      using (SqlCommand SqlCommand = new SqlCommand(" "+ a +" ", myDatabaseConnection)) 
      { 
       int i = 0; 
       SqlDataReader DR1 = SqlCommand.ExecuteReader(); 
       while (DR1.Read()) 
       { 
        i++; 
        BookUserControl usercontrol = new BookUserControl(); 
        usercontrol.Tag = i; 
        usercontrol.EmpID = DR1["EmpID"].ToString(); 
        usercontrol.Name = (string)DR1["Name"]; 
        flowLayoutPanel1.Controls.Add(usercontrol); 
       } 
      } 
     } 

我如何将限制将在FlowLayoutPanel中显示的记录数?我知道有选择顶部。但是,我将如何做到这一点,例如将显示10条记录,当点击下一个按钮时,将显示接下来的10条记录,当前一个按钮点击时,将显示前10条记录。

+1

什么是数据库? Oracle,Sql Server 2000/2005/2008? – unlimit

+0

帮助? http://stackoverflow.com/a/658570/1134076 –

+0

sql server 2008 :) –

回答

1

在SQL Server 2008中,你可以使用Row_Number()

Select * from  
(Select row_number() over (order by EmpID) r, EmpID, Name from EmpTable) X 
where X.r between @start and @end 

供应的@start@end的价值观和你都设置。您可能会也可能不会显示r的值。要从1-10显示,请设置@start = 1@end = 10依此类推。在这个查询中,数据是在EmpID上订购的。

修改后的代码:

string a = "Select * from (Select row_number() over (order by EmpID) r, EmpID, Name from EmpTable) X where X.r between @start and @end"; 
     using (SqlCommand SqlCommand = new SqlCommand(" "+ a +" ", myDatabaseConnection)) 
     { 
      SqlCommand.Parameters.Add("@start").Value = 1; 
      SqlCommand.Parameters.Add("@end").Value = 10; 
      int i = 0; 
      SqlDataReader DR1 = SqlCommand.ExecuteReader(); 
      while (DR1.Read()) 
      { 
       i++; 
       BookUserControl usercontrol = new BookUserControl(); 
       usercontrol.Tag = i; 
       usercontrol.EmpID = DR1["EmpID"].ToString(); 
       usercontrol.Name = (string)DR1["Name"]; 
       flowLayoutPanel1.Controls.Add(usercontrol); 
      } 
     } 

http://msdn.microsoft.com/en-us/library/ms186734(v=sql.90).aspx

+0

谢谢你unlimit :) –

0

是有SELECT TOP在SQL

使用这样

Select TOP(10) EmpID, Name from EmpTable 

我更新你的鳕鱼

flowLayoutPanel1.Controls.Clear(); 
     using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString)) 
     { 
      myDatabaseConnection.Open(); 
      string a = "SELECT TOP(10) EmpID, Name FROM EmpTable"; 
      using (SqlCommand SqlCommand = new SqlCommand(" "+ a +" ", myDatabaseConnection)) 
      { 
       int i = 0; 
       SqlDataReader DR1 = SqlCommand.ExecuteReader(); 
       while (DR1.Read()) 
       { 
        i++; 
        BookUserControl usercontrol = new BookUserControl(); 
        usercontrol.Tag = i; 
        usercontrol.EmpID = DR1["EmpID"].ToString(); 
        usercontrol.Name = (string)DR1["Name"]; 
        flowLayoutPanel1.Controls.Add(usercontrol); 
       } 
      } 
     } 

现在使用这个鳕鱼和这正是你想要

工作

祝你好运

+0

接下来的前10名呢?前十强? –

+0

这不行!当他点击下一个时,这只会重复相同的前10名。他所追求的是SQL分页,或者可能将所有结果加载到列表<>中,然后使用.Take(10)等。 –

+2

如果您想创建像NEXT这样的系统,那么只有top(n)记录 –

0

您最优雅是使用ROW_NUMBER函数

WITH NumberedMyTable AS 
(
    SELECT 
     Id, 
     Value, 
     ROW_NUMBER() OVER (ORDER BY Id) AS RowNumber 
    FROM 
     MyTable 
) 
SELECT 
    Id, 
    Value 
FROM 
    NumberedMyTable 
WHERE 
    RowNumber BETWEEN @From AND @To 

好运