经过大量研究,我发现只有一篇其他帖子提出了这个问题。为了完整起见,这里是 url:
Incorrect results when looping over recordset using Webmatrix/Mono/MySQL
在以各种方式进行测试后,似乎问题在于 mono 没有正确执行 sql 查询。鉴于似乎没有其他人将此记录为问题,我只能假设这是我安装中的错误。
无论如何,作为一种解决方法,这是我使用 webgrid 的解决方案,以防其他人可能感兴趣。
@using System.Collections;
@using System;
@using System.Data;
@using System.Data.SqlClient;
@using System.Dynamic;
@using System.Text;
@using System.Configuration;
@using MySql.Data.MySqlClient;
@{
//establish a connection to mysql db using the connection in web.config
MySqlConnection dbConn = new MySqlConnection(ConfigurationManager.ConnectionStrings["myStringName"].ConnectionString);
//====From here, source data is member table in database=====
//create a mysql command var to store the sql query and reference to the connection string
MySqlCommand command1 = new MySqlCommand("SELECT Id, Fname, Lname, Company, Email FROM ClientTable", dbConn);
//create a mysql adapter to call the command to be executed
MySqlDataAdapter dap1 = new MySqlDataAdapter(command1);
//create a dataset to capture the results of the sql query
DataSet dataset1 = new DataSet();
//use the adapter to fill data with the results of the query.
//ClientTable will be the name of the table in dataset.
dap1.Fill(dataset1, "ClientTable");
//iterate dataset to store its info into a list with columnnames
var clientProfile = new List<dynamic>();
foreach (DataRow dr in dataset1.Tables["ClientTable"].Rows)
{
var obj = (IDictionary<string, object>)new ExpandoObject();
foreach (DataColumn col in dataset1.Tables["ClientTable"].Columns)
{
obj.Add(col.ColumnName, dr[col.ColumnName]);
}
clientProfile.Add(obj);
}
WebGrid grid = new WebGrid(clientProfile, rowsPerPage:10);
}
<div id="xyz">
@grid.GetHtml(tableStyle : "gridtable",
alternatingRowStyle: "altRow")
</div>
嗯,就是这样。希望它有用。