【问题标题】:How can I implement paging for DataGridView in Winform scenario when using Entity Framework?使用Entity Framework时如何在Winform场景下实现DataGridView的分页?
【发布时间】:2013-08-20 08:36:18
【问题描述】:

我可以在网上找到的所有内容都是为 asp.net 使用的。 我想要的只是每次只从数据库中检索记录的页数。这不可能吗?

瓶颈似乎是上下文的处理。不知道如何解决这个问题。

谢谢。
妮可

【问题讨论】:

    标签: winforms entity-framework datagridview paging


    【解决方案1】:

    如果你只需要一个简单的 sql 查询,你可以使用这个例子:

    using System;
    using System.Data.SqlClient;
    
    class Program
    {
    static void Main()
    {
        //
        // The name we are trying to match.
        //
        string dogName = "Fido";
        //
        // Use preset string for connection and open it.
        //
        string connectionString = ConsoleApplication1.Properties.Settings.Default.ConnectionString;
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            //
            // Description of SQL command:
            // 1. It selects all cells from rows matching the name.
            // 2. It uses LIKE operator because Name is a Text field.
            // 3. @Name must be added as a new SqlParameter.
            //
            using (SqlCommand command = new SqlCommand("SELECT count(*) FROM Dogs1 WHERE Name LIKE @Name", connection))
            {
                //
                // Add new SqlParameter to the command.
                //
                command.Parameters.Add(new SqlParameter("Name", dogName));
                //
                // Read in the SELECT results.
                //
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    int count = reader[0];
                }
    
                // Call Close when done reading.
                reader.Close();
            }
        }
    }
    }
    

    请记住,您需要在数据库中找到 connectionString。取决于您拥有的数据库,您可以使用 google 来了解如何操作。

    【讨论】:

    • 我不认为你的问题是正确的。我不是在问如何获取某些查询的计数。我想每次只获取一页的数据量。
    • @Nico “一页数据量”是什么意思?
    • 例如,如果我计划在一页中显示 10 条记录,那么我只想在用户每次单击 Next/Previous/Last/First 按钮时从数据库中返回 10 条记录。明白了吗?
    • @Nico 现在我做到了。你需要的是 ROW_NUMBER()。这个例子会帮助你 - stackoverflow.com/questions/3869311/…
    • 我期待一个解决方案,将 Winform 中的 DGV 使用与 EF 集成。无论如何,我设法弄清楚了。无论如何,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多