【问题标题】:WebMatrix 3 - WebGrid and ADO.NET Dataset with DB2WebMatrix 3 - 带有 DB2 的 WebGrid 和 ADO.NET 数据集
【发布时间】:2013-12-03 20:41:59
【问题描述】:

我正在使用 WebMatrix 查询 DB2 服务器。我认为快速、概念证明相关的任务会更容易……但至少可以说,使用 DB2 而不是它的内置提供程序一直是个挑战。

我知道我的结果会回来,因为我可以很好地遍历我的数据集并查看我的结果。

我正在尝试使用 WebMatrix 中的内置 WebGrid() 函数来显示我的结果并不断收到错误:

CS1502:最佳重载方法匹配 'System.Web.Helpers.WebGrid.WebGrid(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable,字符串,int,bool, bool, string, string, string, string, string, string, string)' 有 一些无效的参数

我的代码基本上是:

OleDbConnection conn = new OleDbConnection(connString);
DataTable td = new DataTable;
OleDbDataAdapter da = new OleDbDataAdapter("SELECT USER, GROUP FROM DBO.TEST", connString);
ds = new DataSet();
da.Fill(ds);

var results = new WebGrid(ds.Tables[0].Rows);

当然,它失败了:

var results = new WebGrid(ds.Tables[0].Rows);

任何帮助或指导将不胜感激。我假设我需要将其转换为 System.Collections.Generic.IEnumerable,但不知道如何完成此操作...?

【问题讨论】:

    标签: db2 webmatrix webgrid


    【解决方案1】:

    您不能将 DataTable 传递到 WebGrid。 WebGrid 需要一个具有公共属性的 Enumerable,它可以用作列名。可以用Linq To DataSet将DataTable的内容投影成更合适的形式。 WebGrid 将接受anonymous type:

    OleDbConnection conn = new OleDbConnection(connString);
    DataTable td = new DataTable;
    string query = @"SELECT USER, GROUP FROM DBO.TEST";
    OleDbDataAdapter da = new OleDbDataAdapter(query, connString);
    da.Fill(dt);
    var data = dt.AsEnumerable().Select(r => new { 
            User = r.Field<string>("USER"),
            Group = r.Field<string>("GROUP")
        });
    
    WebGrid results = new WebGrid(data);
    

    【讨论】:

      猜你喜欢
      • 2012-06-23
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      相关资源
      最近更新 更多