HelloZeno 2018-12-15 08:50 采纳率: 0%
浏览 460

C#使用ajax连接SQL Server时候出现了错误

C#使用ajax连接SQL Server数据库的时候有错误

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>

ajax

$(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)
            }
        })
    });
})

服务器页面端出现问题

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 List<StudInfo> CheckStudInfo(string checkInput)
        {
            SqlConnection sqlConnection = new SqlConnection();
            string checkSQL = "select * from dbo.StudInfo where StudName like '%" + checkInput + "%'";
            List<StudInfo> list = sqlConnection.Database.SqlQuery<StudInfo>(checkSQL).ToList();
            return list;
        }
    }
}

提示错误

  • 写回答

2条回答

  • threenewbee 2018-12-15 13:36
    关注

    StudInfo,这个类没有找到,可能的原因是
    类型写错了
    缺少using,导致包含StudInfo的命名空间没有导入
    忘记定义StudInfo类型了。

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?