Winforms 在使用实体框架时,如何在Winform场景中实现DataGridView分页?
我在网上所能找到的都是asp.net使用的。 我只想每次从数据库中检索记录的页数。这不可能吗 瓶颈似乎是对上下文的处理。不知道如何解决这个问题 谢谢。Winforms 在使用实体框架时,如何在Winform场景中实现DataGridView分页?,winforms,entity-framework,datagridview,paging,Winforms,Entity Framework,Datagridview,Paging,我在网上所能找到的都是asp.net使用的。 我只想每次从数据库中检索记录的页数。这不可能吗 瓶颈似乎是对上下文的处理。不知道如何解决这个问题 谢谢。 Nico如果您只需要一个简单的sql查询,可以使用以下示例: using System; using System.Data.SqlClient; class Program { static void Main() { // // The name we are trying to match. // stri
Nico如果您只需要一个简单的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();
}
}
}
}
请记住,您需要找到数据库的
连接字符串。取决于您拥有的数据库,您可以使用google来了解如何进行查询。如果您只需要一个简单的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();
}
}
}
}
请记住,您需要找到数据库的连接字符串。取决于你拥有什么样的数据库,你可以使用谷歌来找出如何做到这一点。我认为你没有答对我的问题。我不是问如何获得某个查询的计数。我希望每次只获取一页数据。@Nico“一页数据”是什么意思?例如,如果我计划在一页中显示10条记录,那么每次用户单击“下一个/上一个/最后一个/第一个”按钮时,我只希望从数据库返回10条记录。明白了吗?尼科,我明白了。您需要的是行号()。这个示例将帮助您-我希望有一个解决方案将winform中DGV的使用与EF集成。不管怎样,我还是设法弄明白了。不管怎样,谢谢。我想你没有答对我的问题。我不是问如何获得某个查询的计数。我希望每次只获取一页数据。@Nico“一页数据”是什么意思?例如,如果我计划在一页中显示10条记录,那么每次用户单击“下一个/上一个/最后一个/第一个”按钮时,我只希望从数据库返回10条记录。明白了吗?尼科,我明白了。您需要的是行号()。这个示例将帮助您-我希望有一个解决方案将winform中DGV的使用与EF集成。不管怎样,我还是设法弄明白了。无论如何,谢谢你。