如何将Sql Server数据库数据显示到W2ui网格控件中 [英] How Do I Display Sql Server Database Data Into W2ui Grid Control

查看:83
本文介绍了如何将Sql Server数据库数据显示到W2ui网格控件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想通过将数据转换为json格式来显示从sql server到w2ui网格的数据但是我收到错误数据不是有效的json格式...请帮帮我怎样才能把远程数据变成w2ui网格

提前谢谢..



w2uigrid.aspx中的代码我已经声明了w2ui网格

Hi everyone,
I want to display data from sql server to w2ui grid by converting data into json format but I got an error "data is not in a valid json format" ...please help me how can i dispaly remote data into w2ui grid
Thanks in advance..

code in w2uigrid.aspx in which i have declared the w2ui grid

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="databind.aspx.cs" Inherits="databind" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script type="text/javascript" src="http://w2ui.com/src/w2ui-1.4.min.js"></script>
  <link rel="stylesheet" type="text/css" href="http://w2ui.com/src/w2ui-1.4.min.css" />
</head>
<body>

    <div id="grid" style="width: 100%; height: 350px;"></div>

<script type="text/javascript">
    $(function () {
        $('#grid').w2grid({
            name: 'grid',

          url:'json.aspx/ConvertDataTabletoString',
           
           columns: [
            { field: 'recid', caption: 'recid', size: '30%' },

            { field: 'fname', caption: 'First Name', size: '30%' },
            { field: 'lname', caption: 'Last Name', size: '30%' },
            { field: 'email', caption: 'Email', size: '40%' },
           { field: 'sdate', caption: 'sdate', size: '40%' }

            ]
          
        });
    });

</script>

</body>
</html>





json.aspx.cs中的代码,其中我编写了将数据转换为json格式的代码

使用System;

使用System.Collections .Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Data;

使用System.Data.SqlClient;

使用System。配置;

公共部分类json:System.Web.UI.Page

{

protected void Page_Load(object sender,EventAr gs e)

{

var c = ConvertDataTabletoString();



}





public DataTable ConvertDataTabletoString()

{

DataTable dt = new DataTable();

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [databoundConnectionString]。ConnectionString);



SqlCommand cmd = new SqlCommand(JSONProcas2, con);

var x = cmd.CommandType = CommandType.StoredProcedure;



con.Open();

SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(dt);

return dt;

}

}

存储过程是



Code in json.aspx.cs in which i have written code for converting data into json format
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class json : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var c=ConvertDataTabletoString();

}


public DataTable ConvertDataTabletoString()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["databoundConnectionString"].ConnectionString);

SqlCommand cmd = new SqlCommand("JSONProcas2", con);
var x= cmd.CommandType=CommandType.StoredProcedure;

con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
return dt;
}
}
Stored procedure is

create procedure [dbo].[JSONProcas2]
as
begin
select '{records:[' + STUFF((
           select
            ',{"recid":' + cast(recid as varchar(max))
            +',"id":' + cast(stuid as varchar(max))
             + ',"name":"' + stuname + '"'
             +'}'

              from student2 t1
              for xml path(''), type

             ).value('.', 'varchar(max)'), 1, 1, '') + ']}'
end

推荐答案

function (){


' #grid')。w2grid ({
name:' grid'

url: ' json.aspx / ConvertDataTabletoString'

列:[
{field:' recid',标题:' recid',大小: ' 30%'},

{field:' fname',标题:' 名字',大小:' 30%'},
{field:' lname',标题:' 姓氏',大小:' 30 %'},
{字段:' 电子邮件',标题:' 电子邮件',大小:' 40%'},
{field:' sdate',标题:' sdate',大小:' 40%'}

]

});
});

< / script >

< / body >
< / html >
('#grid').w2grid({ name: 'grid', url:'json.aspx/ConvertDataTabletoString', columns: [ { field: 'recid', caption: 'recid', size: '30%' }, { field: 'fname', caption: 'First Name', size: '30%' }, { field: 'lname', caption: 'Last Name', size: '30%' }, { field: 'email', caption: 'Email', size: '40%' }, { field: 'sdate', caption: 'sdate', size: '40%' } ] }); }); </script> </body> </html>





json.aspx.cs中的代码,其中我编写了将数据转换为json格式的代码

使用System;

使用System.Collections .Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Data;

使用System.Data.SqlClient;

使用System。配置;

公共部分类json:System.Web.UI.Page

{

protected void Page_Load(object sender,EventAr gs e)

{

var c = ConvertDataTabletoString();



}





public DataTable ConvertDataTabletoString()

{

DataTable dt = new DataTable();

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [databoundConnectionString]。ConnectionString);



SqlCommand cmd = new SqlCommand(JSONProcas2, con);

var x = cmd.CommandType = CommandType.StoredProcedure;



con.Open();

SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(dt);

return dt;

}

}

存储过程是



Code in json.aspx.cs in which i have written code for converting data into json format
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class json : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var c=ConvertDataTabletoString();

}


public DataTable ConvertDataTabletoString()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["databoundConnectionString"].ConnectionString);

SqlCommand cmd = new SqlCommand("JSONProcas2", con);
var x= cmd.CommandType=CommandType.StoredProcedure;

con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
return dt;
}
}
Stored procedure is

create procedure [dbo].[JSONProcas2]
as
begin
select '{records:[' + STUFF((
           select
            ',{"recid":' + cast(recid as varchar(max))
            +',"id":' + cast(stuid as varchar(max))
             + ',"name":"' + stuname + '"'
             +'}'

              from student2 t1
              for xml path(''), type

             ).value('.', 'varchar(max)'), 1, 1, '') + ']}'
end


您的方法不返回JSON对象,它返回一个数据表。您需要将数据表序列化为JSON对象。



http://www.aspdotnet-suresh.com/2013/05/c-convert-datatable-to-json-string-in-c.html [ ^ ]
Your method does not return a JSON object, it returns a datatable. You need to serialize the datatable to JSON object.

http://www.aspdotnet-suresh.com/2013/05/c-convert-datatable-to-json-string-in-c.html[^]


这篇关于如何将Sql Server数据库数据显示到W2ui网格控件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆