2

私はウェブマトリックスを使用しています。私のホーム サーバーは Ubuntu/mono/nginx/fastcgi です。私のコードは単純です。4 つのテスト レコードを含むテーブルを持つ MySql データベースがあります。

@{
        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 を削除してテーブルを作成しましたが、結果は同じでした。同じ結果で他のデータベースでテストしたので、問題はデータベースではないようです。また、サーバー上で(パテを使用して)プローブなしでクエリを実行したため、クエリは機能するはずです。私は答えを広範囲に検索しました。提供された支援に感謝します。前もって感謝します。

4

2 に答える 2

1
<div id="grid">
    @grid.GetHtml()
</div>

IDで試してみるか、うまくいかない場合はdivを削除してください。グリッドがループを実行して結果を表示し、反復ごとに前の結果が上書きされるような気がします。

于 2013-09-08T12:55:52.657 に答える
0

多くの調査の結果、この問題を提起した投稿は他に 1 つしかないことがわかりました。完全を期すために、ここに URL があります: 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>    

それだけです。それが役に立つことを願っています。

于 2013-09-19T07:46:55.600 に答える