在webmethod中添加列名

时间:2016-07-20 12:37:04

标签: jquery sql linq webmethod

我有SQL查询然后我转换这个LINQ然后我在jquery中绑定gridview。现在SQL查询中有一个没有名称的列。

正如您在结果中看到"("没有列名)"现在我想给这个专栏命名,我也是如何在代码和jquery

中添加此列

我有像这样的SQL查询

ALTER procedure [dbo].[grid_data]
    @fromdate datetime,
    @todate datetime,
    @region varchar(50)
as
    Select D.ID as 
        ID, 
        (Select Count(*) from tblve WHERE MID = D.ID and Vme <> '') as                   
        total, 
        D.lim,
        D.Speed 
    from tblRe M 
        inner join tblReg D 
        On M.RID = D.RID 

并返回此类数据

ID      (No column name)  lim   Speed
19235       3             1065   120
19655       5             923    120
20261       4             757    120
19659       4             551    122

更新

然后我将此转换为linq像这样..现在的问题是当我添加此列&#34;总计&#34;然后当我按f12控制台显示&#34; http://localhost:33578/WebForm1.aspx/search_data Failed to load resource: the server responded with a status of 500 (Internal Server Error)"

[WebMethod]
public static string search_data(DateTime fromdate, DateTime todate, string region)
{ 
    try
    { 
        T1 ts = new T1();
        var query = (
            from M in ts.tblRe 
                join D in ts.tblReg on M.RID equals D.RID
            where 
                M.Region == region
                && M.StartDate <= fromdate
                && M.EndDate >= todate
            select new { 
                D.ID,
                total= ts.tblve.Where(x => x.MID == D.ID && x.Vme != 
              '').Count()
                D.lim,
                D.Speed
            }).ToList();

        DataTable dt = new DataTable();

        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("total", typeof(int));
        dt.Columns.Add("lim", typeof(string));
        dt.Columns.Add("Speed", typeof(string));

        foreach (var c in dq)
        {
            dt.Rows.Add(c.ID,c.total, c.lim, c.Speed);
        }

        result = DataSetToJSON(dt);

        return result;
    }
    catch (Exception)
    {
        throw new Exception("");
    }    
}

然后我添加列并在webmethod和jquery中绑定gridview,就像这样

success: function (result) {
    var final = JSON.parse(result.d).response;
    console.log(JSON.parse(result.d).response)
    $("#tabledata").empty();

    if (final.length > 0) {
        $("#tabledata").append("<tr><th>ID</th><th>total</th><th>lim</th><th>Speed</th></tr>");

        for (var i = 0; i < final.length; i++) {
            if (final[i] !== null) {
                $("#tabledata").append("<tr><td>" +
                    final[i][0] + "</td> <td>" +
                    final[i][1] + "</td> <td>" +
                    final[i][2] + "</td> <td>" +
                    final[i][3] + "</td></tr>");    
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

@EdPlunkett解决方案对我有用

dt.Columns.Add(“ Count”,typeof(int));