【问题标题】:Sql select query is only returning last row n timesSql select查询只返回最后一行n次
【发布时间】:2013-09-12 02:12:21
【问题描述】:

我正在使用网络矩阵。我的家庭服务器是 Ubuntu/mono/nginx/fastcgi。我的代码很简单。 我有一个 MySql 数据库,其中包含一个包含 4 条测试记录的表。

@{
        var db = Database.Open("wra");
        var sql = "SELECT * FROM Clients";
        var clientinfo = db.Query(sql);
        WebGrid grid = new WebGrid(clientinfo);
}

<div>
    @grid.GetHtml()
</div>

就是这样 - 并没有变得更简单。但是,网格仅返回最后一条记录并显示 4 次(= 记录数)。我已经用其他数据库和表对此进行了测试,结果相同。没有错误,所以没有堆栈跟踪。 问题似乎不是 webgrid,因为它只显示结果。可以肯定的是,我删除了 webgrid 并创建了一个表格 - 结果相同。 问题似乎不是数据库,因为我已经用其他数据库测试了相同的结果。我还在服务器上运行了查询(使用腻子),没有探测,所以查询应该可以工作。 我已经广泛搜索了答案。我将不胜感激提供的任何帮助。提前致谢。

【问题讨论】:

  • 看起来像单声道中的错误。
  • 感谢您的回复迈克。我得出同样的结论。我在我的 Windows 机器上对其进行了测试,并且查询正确执行。我可能必须编写与我试图避免的 sql 查询等效的内容。还有其他建议吗?
  • 我不太了解单声道应该支持什么。 .NET 4.0,但数据库助手和 WebGrid 都依赖于相对较新的 dynamic 类型。我的猜测是这是问题的原因。尝试使用 ADO.NET 并从 SqlDataReader 生成您自己的 HTML 表。
  • 再次感谢。我会试试看的。

标签: mysql sql select webmatrix webgrid


【解决方案1】:
<div id="grid">
    @grid.GetHtml()
</div>

用id试试,如果不行就去掉div。我感觉grid运行循环显示结果,每次迭代都会覆盖之前的结果。

【讨论】:

  • 感谢您的回复。我尝试了一个 id,然后没有 div。结果相同。如前所述,我认为这不是 webgrid 的问题。我删除了 webgrid 并将其替换为使用 foreach 循环并给出相同结果的表。我认为这是查询本身未正确处理的问题,这确实很不寻常。
【解决方案2】:

经过大量研究,我发现只有一篇其他帖子提出了这个问题。为了完整起见,这里是网址: 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>    

嗯,就是这样。希望有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    相关资源
    最近更新 更多