限制记录

时间:2013-07-25 06:51:59

标签: c# sql sql-server-2008 ado.net

我有这个功能,可以将数据库中的记录显示到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条记录。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);
                }
            }
        }

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

祝你好运

答案 2 :(得分: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
祝你好运