帮我写一个 用ajax实现增删改查前后端,,使用html文件、js文件中应用ajax为前端,一般处理程序ashx为后端,使用SqlServer数据库
时间: 2024-05-09 09:21:53 浏览: 156
好的,我理解你的要求。以下是一个简单的示例,用ajax实现增删改查前后端,使用html文件、js文件中应用ajax为前端,一般处理程序ashx为后端,使用SqlServer数据库:
前端HTML文件:
```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>使用ajax实现增删改查</title>
</head>
<body>
<h1>使用ajax实现增删改查</h1>
<h2>增加数据</h2>
<input type="text" id="name" placeholder="姓名">
<input type="text" id="age" placeholder="年龄">
<button onclick="add()">确定</button>
<h2>修改数据</h2>
<input type="text" id="id" placeholder="id">
<input type="text" id="newName" placeholder="新姓名">
<input type="text" id="newAge" placeholder="新年龄">
<button onclick="update()">确定</button>
<h2>删除数据</h2>
<input type="text" id="delId" placeholder="要删除的id">
<button onclick="del()">确定</button>
<h2>查找数据</h2>
<input type="text" id="findId" placeholder="要查找的id">
<button onclick="find()">确定</button>
<h2>所有数据</h2>
<button onclick="findAll()">确定</button>
<div id="result"></div>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script src="script.js"></script>
</body>
</html>
```
前端JS文件:
```js
function add() {
var name = $("#name").val();
var age = parseInt($("#age").val());
$.ajax({
url: "handler.ashx",
type: "POST",
data: {
method: "add",
name: name,
age: age
},
success: function(result) {
$("#result").html(result);
}
});
}
function update() {
var id = parseInt($("#id").val());
var name = $("#newName").val();
var age = parseInt($("#newAge").val());
$.ajax({
url: "handler.ashx",
type: "POST",
data: {
method: "update",
id: id,
name: name,
age: age
},
success: function(result) {
$("#result").html(result);
}
});
}
function del() {
var id = parseInt($("#delId").val());
$.ajax({
url: "handler.ashx",
type: "POST",
data: {
method: "del",
id: id
},
success: function(result) {
$("#result").html(result);
}
});
}
function find() {
var id = parseInt($("#findId").val());
$.ajax({
url: "handler.ashx",
type: "POST",
data: {
method: "find",
id: id
},
success: function(result) {
$("#result").html(result);
}
});
}
function findAll() {
$.ajax({
url: "handler.ashx",
type: "POST",
data: {
method: "findAll"
},
success: function(result) {
$("#result").html(result);
}
});
}
```
后端处理程序ashx文件:
```cs
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Data.SqlClient;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string method = context.Request.Form["method"];
if (method == "add") {
string name = context.Request.Form["name"];
int age = Convert.ToInt32(context.Request.Form["age"]);
Add(name, age);
context.Response.Write("增加成功");
} else if (method == "update") {
int id = Convert.ToInt32(context.Request.Form["id"]);
string name = context.Request.Form["name"];
int age = Convert.ToInt32(context.Request.Form["age"]);
Update(id, name, age);
context.Response.Write("修改成功");
} else if (method == "del") {
int id = Convert.ToInt32(context.Request.Form["id"]);
Del(id);
context.Response.Write("删除成功");
} else if (method == "find") {
int id = Convert.ToInt32(context.Request.Form["id"]);
string result = Find(id);
context.Response.Write(result);
} else if (method == "findAll") {
string result = FindAll();
context.Response.Write(result);
} else {
context.Response.Write("输入错误的方法");
}
}
public bool IsReusable {
get {
return false;
}
}
private void Add(string name, int age) {
string connectionString = "Server=(localdb)\\mssqllocaldb;Database=mydatabase;Trusted_Connection=True;";
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand command = new SqlCommand("INSERT INTO mytable(name, age) VALUES(@name, @age)", connection);
command.Parameters.AddWithValue("@name", name);
command.Parameters.AddWithValue("@age", age);
command.ExecuteNonQuery();
}
}
private void Update(int id, string name, int age) {
string connectionString = "Server=(localdb)\\mssqllocaldb;Database=mydatabase;Trusted_Connection=True;";
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand command = new SqlCommand("UPDATE mytable SET name=@name, age=@age WHERE id=@id", connection);
command.Parameters.AddWithValue("@id", id);
command.Parameters.AddWithValue("@name", name);
command.Parameters.AddWithValue("@age", age);
command.ExecuteNonQuery();
}
}
private void Del(int id) {
string connectionString = "Server=(localdb)\\mssqllocaldb;Database=mydatabase;Trusted_Connection=True;";
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand command = new SqlCommand("DELETE FROM mytable WHERE id=@id", connection);
command.Parameters.AddWithValue("@id", id);
command.ExecuteNonQuery();
}
}
private string Find(int id) {
string connectionString = "Server=(localdb)\\mssqllocaldb;Database=mydatabase;Trusted_Connection=True;";
string result = "";
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand command = new SqlCommand("SELECT * FROM mytable WHERE id=@id", connection);
command.Parameters.AddWithValue("@id", id);
SqlDataReader reader = command.ExecuteReader();
if (reader.Read()) {
string name = reader["name"].ToString();
int age = Convert.ToInt32(reader["age"]);
result = String.Format("姓名:{0} 年龄:{1}", name, age);
}
}
return result;
}
private string FindAll() {
string connectionString = "Server=(localdb)\\mssqllocaldb;Database=mydatabase;Trusted_Connection=True;";
string result = "";
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand command = new SqlCommand("SELECT * FROM mytable", connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read()) {
int id = Convert.ToInt32(reader["id"]);
string name = reader["name"].ToString();
int age = Convert.ToInt32(reader["age"]);
result += String.Format("id:{0} 姓名:{1} 年龄:{2}<br>", id, name, age);
}
}
return result;
}
}
```
你可以在SqlServer数据库中创建一个名为mydatabase的数据库,并在其中创建一个名为mytable的表,包含id(int),name(varchar),age(int)三个字段。
希望这个示例对你有所帮助。如果有任何问题或需要进一步帮助,请随时问我。
阅读全文