【发布时间】:2017-10-30 13:57:44
【问题描述】:
我不明白服务器端分页如何与 C# MVC 中的 MySql 和 Datatable 一起使用。
我在 C# 中创建了一个 Controlled,在其中我建立了与 MySql 数据库的连接(为了做到这一点,我遵循了这个示例):
public ActionResult connectDB()
{
const string DB_CONN_STR = "Server=MyServer;Port=MyPort;Uid=MyUid;Database=MyDB;";
MySqlConnection cn = new MySqlConnection(DB_CONN_STR);
try
{
string sqlCmd = "select * from t_documento";
MySqlDataAdapter adr = new MySqlDataAdapter(sqlCmd, cn);
adr.SelectCommand.CommandType = CommandType.Text;
DataTable dt = new DataTable();
adr.Fill(dt); //opens and closes the DB connection automatically !! (fetches from pool)
return Content(JsonConvert.SerializeObject(dt).ToString());
}
catch (Exception ex)
{
Console.WriteLine("{oops - {0}", ex.Message);
return Content(ex.ToString());
}
finally
{
cn.Dispose(); // return connection to pool
}
}
但是,通过这种方式,我检索了存储在该表中的所有记录,但我想通过实现分页来填充数据表(我的数据表的初始化位于 cshtml 页面中)。 我阅读了很多文章,但没有找到一个明确的 MySql DB 示例。 有人能帮助我吗? 谢谢!
【问题讨论】:
-
您可以使用(存储的)过程来执行此操作。只需将页码添加到您的控制器并将其传递给模型和过程。以下是 SQL Server 中分页的一些很好的参考:A More Efficient Method for Paging Through Large Result Sets 和 Row Offset in SQL Server。我相信 MySql DB 会非常相似。
标签: c# mysql model-view-controller pagination datatables