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;
}
}
}