HelloZeno 2018-12-15 10:00 采纳率: 0%
浏览 1487

C#查询数据库后通过ajax把内容显示在网页上要怎么做?

html页面

    <h2>查询学生</h2>
    <input type="text" id="checkInput" placeholder="请输入要查询学生的姓名关键词:" style="width:200px;height:50px" />
    <button type="button" id="checkBtn" style="width:80px;height:55px">查询</button>
    <h2>显示学生</h2>
    <div id="showDiv"></div>

js代码

$(document).ready(function () {
    $("#checkBtn").click(function () {
        var checkInput = $("#checkInput").val();
        var checkInputJson = "{'checkInput':'checkInput'}"

        $.ajax({
            type: "Post",
            url: "Server.aspx/CheckStudInfo",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: checkInputJson,
            success: function (data) {
                $("#showDiv").html(data.d);
            },
            error: function (err) {
                alert(err)
            }
        })
    });
})

服务器端页面C#总是错误,不知道该怎么入手,求高手

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class Server : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string CheckStudInfo(string checkInput)
        {
            SqlConnection sqlConnection = new SqlConnection();
            string checkSQL = "select * from dbo.StudInfo where StudName like '%" + checkInput + "%'";
            //下面应该如何入手?写出来报错,没有头绪
        }
    }
}
  • 写回答

2条回答 默认 最新

  • cising1689 2018-12-17 05:58
    关注

    把查询出来的数据转换成Json格式
    var json = DataTableToJsonWithJavaScriptSerializer(dt);
    Response.Write(json);
    Response.End();

    #region dataTable通过 JavaScriptSerializer 转换成Json格式
    public string DataTableToJsonWithJavaScriptSerializer(DataTable table)
    {
    JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
    List> parentRow = new List>();
    Dictionary childRow;
    foreach (DataRow row in table.Rows)
    {
    childRow = new Dictionary();
    foreach (DataColumn col in table.Columns)
    {
    childRow.Add(col.ColumnName, row[col]);
    }
    parentRow.Add(childRow);
    }

            return jsSerializer.Serialize(parentRow);
        }
        #endregion 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀