1.查询
context.Response.ContentType = "text/html";
StringBuilder sb = new StringBuilder(); 注:定义一个长度可变的字符串
sb.Append(@"</head>
<body>
<a href='increase.html'>添加</a>
<table width='600' border='1' cellspacing='0'>
<tr>
<td>学号</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>操作</td>
</tr>");
string sql = "select * from student"; 注:查询的表
SqlDataReader sqls = SQLHelper.ExecuteReader(sql); 注:查询的表变成一种一次读取一行的只进流
while(sqls.Read())
{
sb.AppendFormat(@"<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td><a href='delete.ashx?id={4}' onclick='return confirm("您确定要删除吗?")'>删除</a> <a href='amend.ashx?id={5}' onclick='return confirm("您确定要修改吗?")'>修改</a></td></tr>", sqls["number"], sqls["name"], sqls["sex"], sqls["age"], sqls["number"], sqls["number"]); 注:url?id={5}传输数距,语法是键值对 键1=值1&键2=值2 " HTML转义符先当与 “ 。
}
sb.Append(@"</table></body></html>");
context.Response.Write(sb.ToString()); 注:输出字符串(可变的)。。
2.添加
context.Response.ContentType = "text/html";
string name = context.Request["nam"]; 注:获取name属性为nam
string sex = context.Request["sex"]; 注:获取name属性为sex
string age = context.Request["age"]; 注:获取name属性为age
if (name == "" && sex == null && age == "") 注:判断获得的值是否为空
{
context.Response.Redirect("increase.html"); 注:跳转
}
else
{
string sql = "insert into Student(name,sex,age) values(@name,@sex,@age)";
SqlParameter[] sp =
{
new SqlParameter("@name",name),
new SqlParameter("@sex",sex),
new SqlParameter("@age",age)
};
int i = SQLHelper.ExecuteNonQuery(sql, sp); 注:插入数距
if (i == 1) 注:判断数距是否插入成功
{
context.Response.Redirect("Crud.ashx");
}
}
3.删除
context.Response.ContentType = "text/plain";
string delete = context.Request["id"]; 注:获得传过来的id
if (delete != null) 注:判断id是否为null
{
string sql = "delete Student where number=" + delete; 注:根据id删除
SQLHelper.ExecuteNonQuery(sql);
context.Response.Redirect("Crud.ashx"); 注:跳转
}
4.修改
context.Response.ContentType = "text/html";
string id = context.Request["id"]; 注:获得传过来的id
if (id != null) 注:判断id是否为null,如果不为null
{
string sql = "select * from Student where number=" + id; 注:根据id查询
DataTable dt = SQLHelper.ExecuteDataTable(sql);
string str = File.ReadAllText(context.Request.MapPath("amend.html")); 注:根据相对路径获得绝对路径
str = str.Replace("{number}", dt.Rows[0]["number"].ToString()); 注:将{number}占位符替换成dt.Rows[0]["number"]
str = str.Replace("{nam}", dt.Rows[0]["name"].ToString()); 注:将{nam}占位符替换成dt.Rows[0]["name"]
str = str.Replace("{age}", dt.Rows[0]["age"].ToString()); 注:将{age}占位符替换成dt.Rows[0]["age"]
context.Response.Write(str); 注:输出str
}
else 注:如果为空
{
string number = context.Request["number"]; 注:获取name属性为number
string name = context.Request["nam"]; 注:获取name属性为nam
string sex = context.Request["sex"]; 注:获取name属性为sex
string age = context.Request["age"]; 注:获取name属性为age
if (number == "" && name == "" && sex == null && age == "") 注:判断值是否为空,如果为空
{
context.Response.Redirect("amend.html"); 注:跳转
}
else 注:如果不为空
{
string sql = "update Student set name=@name,sex=@sex,age=@age where number=" + number; 注:根据number修改
SqlParameter[] sp =
{
new SqlParameter("@name",name),
new SqlParameter("@sex",sex),
new SqlParameter("@age",age)
};
int i = SQLHelper.ExecuteNonQuery(sql, sp);
if (i == 1) 注:判断是否修改成功
{
context.Response.Redirect("Crud.ashx");
}
}
}