小编典典

将计数和数据作为json返回extjs网格

sql

如何返回两个Json对象?基本上,我试图从sql db和db中的行数发送一堆数据。

下面的代码发送myData成功,但是现在我也想发送count

string sqlquery = "SELECT Q1, Q2, Q3, Q4, Improvements, Comments FROM myTable";
conn.Open();
SqlDataAdapter cmd = new SqlDataAdapter(sqlquery, conn);
SqlCommand comd = new SqlCommand(sqlquery, conn);
DataSet myData = new DataSet();
cmd.Fill(myData, "myTable");


comd.CommandText = "SELECT COUNT(*) FROM myTable";
Int32 count = (Int32)comd.ExecuteScalar();

comd.ExecuteNonQuery();
conn.Close();

return JsonConvert.SerializeObject(myData, Formatting.Indented,
                        new JsonSerializerSettings
                        {
                            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                        });

这是我的网格

this.grid = Ext.create('Ext.grid.Panel', {
        title: 'GridView App',
        frame: true,
        trackMouseOver: true,
        disableSelection: true,
        autoHeight: true,
        store: store,
        loadMask: true,
        columns: [

        { header: 'Q1',
            sortable: true, dataIndex: 'Q1'
        },
        { header: 'Q2',
            sortable: true, dataIndex: 'Q2'
        },
        { header: 'Q3',
            sortable: true, dataIndex: 'Q3'
        },
        { header: 'Q4',
            sortable: true, dataIndex: 'Q4'
        },
        { header: 'Improvements', flex: 1,
            sortable: true, dataIndex: 'Improvements'
        },
        { header: 'Comments', flex: 1,
            sortable: true, dataIndex: 'Comments'
        }
    ],

这是我的商店

var store = Ext.create('Ext.data.JsonStore', {

        storeId: 'myData',
        scope: this,
        pagesize: itemsPerPage,
        remoteSort: true,
        fields: [
    { name: 'Q1', type: 'int' },
    { name: 'Q2', type: 'int' },
    { name: 'Q3', type: 'int' },
    { name: 'Q4', type: 'int' },
    { name: 'Q5', type: 'int' },
    { name: 'Improvements', type: 'string' },
    { name: 'Comments', type: 'string' }
    ],

        sorters: [
        {
            property: 'Q1',
            direct: 'ASC'
        }
     ],

        proxy: {
            type: 'ajax',
            scope: this,
            url: 'GridView/writeRecord',
            reader: {
                type: 'json',
                root: 'myTable',
                totalProperty: 'count'
            }
        }

    });

阅读 177

收藏
2021-05-16

共1个答案

小编典典

基本上,这就是我在ASP.NET应用程序中的工作方式,同时我也在使用JSON.NET:

string sqlquery = "SELECT Q1, Q2, Q3, Q4, Improvements, Comments FROM myTable";
conn.Open();
SqlDataAdapter cmd = new SqlDataAdapter(sqlquery, conn);
SqlCommand comd = new SqlCommand(sqlquery, conn);
DataSet myData = new DataSet();
cmd.Fill(myData, "myTable");


comd.CommandText = "SELECT COUNT(*) FROM pid_FluSurvey";
Int32 count = (Int32)comd.ExecuteScalar();

comd.ExecuteNonQuery();
conn.Close();

return JsonConvert.SerializeObject( new { myTable = myData.Tables[0], count = count }, Formatting.Indented,
    new JsonSerializerSettings
    {
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    });

在这里重要的是,您可以调用SerializeObject任何匿名类型,并在其中放置您想要/需要的任意数量的属性。

2021-05-16