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 中显示的记录数?我知道有 select top 。但是我将如何做到这一点,例如将显示 10 条记录,当单击下一个按钮时,将显示下一个 10 条记录,当单击上一个按钮时,将显示前 10 条记录。

4

3 回答 3

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。显示 from 1-10, set@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

于 2013-07-25T07:07:09.483 回答
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

祝你好运

于 2013-07-25T07:14:36.967 回答
0

是的,SQL 中有 SELECT TOP

像这样使用它

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);
                }
            }
        }

现在使用这个鳕鱼,这将完全符合您的要求

祝你好运

于 2013-07-25T06:58:31.710 回答