如果不是asp.net webform,怎么进行增删改查呢?
首先:
创建一个SqlHelper.cs
public class SqlHelper { public static readonly string conString = ConfigurationManager.ConnectionStrings["sqlCon"].ConnectionString; //增删改 public static bool ExeNonQuery(string sql, CommandType type, params SqlParameter[] lists) { bool bFlag = false; using (SqlConnection con = new SqlConnection(conString)) { SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = sql; cmd.CommandType = type; if (lists!=null) { foreach (SqlParameter p in lists) { cmd.Parameters.Add(p); } } try { if (con.State == ConnectionState.Closed) { con.Open(); } int result = cmd.ExecuteNonQuery(); if (result > 0) { bFlag = true; } } catch { ;} } return bFlag; } //查.读 public static SqlDataReader ExeDataReader(string sql, CommandType type, params SqlParameter[] lists) { SqlConnection con = new SqlConnection(conString); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = sql; cmd.CommandType = type; if (con.State == ConnectionState.Closed) { con.Open(); } if (lists!=null) { foreach (SqlParameter p in lists) { cmd.Parameters.Add(p); } } SqlDataReader reader = cmd.ExecuteReader(); return reader; } //返回单个值 public static object GetScalar(string sql, CommandType type, params SqlParameter[] lists) { object returnValue = null; using (SqlConnection con = new SqlConnection(conString)) { SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = sql; cmd.CommandType = type; if (lists!=null) { foreach (SqlParameter p in lists) { cmd.Parameters.Add(p); } } try { if (con.State == ConnectionState.Closed) { con.Open(); } returnValue = cmd.ExecuteScalar(); } catch { ; } } return returnValue; } //事务 public static bool ExeNonQueryTran(List<SqlCommand> list) { bool flag = true; SqlTransaction tran = null; using (SqlConnection con = new SqlConnection(conString)) { try { if (con.State == ConnectionState.Closed) { con.Open(); tran = con.BeginTransaction(); foreach (SqlCommand com in list) { com.Connection = con; com.Transaction = tran; com.ExecuteNonQuery(); } tran.Commit(); } } catch (Exception ex) { Console.Write(ex.Message); tran.Rollback(); flag = false; } } return flag; } //返回DataTable public static DataTable GetTable(string sql) { SqlConnection conn = new SqlConnection(conString); SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataTable table = new DataTable(); da.Fill(table); return table; } }
然后再Web.Config里面进行配置
<configuration> <connectionStrings> <add name="sqlCon" connectionString="server=.;database=student;uid=sa;pwd=fiybird"/> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web>
将要操作的表stus
创建一个html页面List.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="JS/jquery-1.8.3.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $("a:contains('删除')").click(function () { if (!confirm("你确定要删除么")) { return false; } }) }) </script> </head> <body> <table border="1" cellspacing="0" cellpadding="0" width="500"> <tr> <th>编号</th> <th>姓名</th> <th>性别</th> <th>班级</th> <th>操作(删除)</th> <th>操作(查看)</th> <th>操作(修改)</th> </tr> {body}//这里是不是看不懂,先别急,往下看 </table> </body> </html> 接着创建一个一般处理程序List.ashx public class List : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; // string path = context.Server.MapPath("List.html"); string strHtml = Commd.getFile("List.html"); context.Response.Write(strHtml.Replace("{body}", Getpost()));//把得到的数据和{body}进行替换 } string Getpost() { StringBuilder sb = new StringBuilder(); List<Stus> list = getAll(); foreach (Stus stu in list) { sb.AppendFormat("<tr>"); sb.AppendFormat("<td>" + stu.id + "</td>"); sb.AppendFormat("<td>"+stu.name+"</td>"); sb.AppendFormat("<td>" + stu.sex + "</td>"); sb.AppendFormat("<td>" + stu.c_id + "</td>"); sb.AppendFormat("<td><a href='Del.ashx?id={0}'>删除</a></td>",stu.id); sb.AppendFormat("<td><a href='Dils.ashx?id={0}'>查看</a></td>", stu.id); sb.AppendFormat("<td><a href='Edit.ashx?id={0}'>修改</a></td>", stu.id); sb.AppendFormat("</tr>"); } return sb.ToString(); } public List<Stus> getAll()//查询得到所有的数据 { string sql=string.Format("select * from stus"); List<Stus> list = new List<Stus>(); IDataReader red = SqlHelper.ExeDataReader(sql, CommandType.Text, null); while (red.Read()) { Stus s = new Stus { id = int.Parse(red[0].ToString()), name = red[1].ToString(), sex = red[2].ToString(), c_id = int.Parse(red[3].ToString()) }; list.Add(s); } return list; } public class Stus//创建类 { public int id { get; set; } public string name { get; set; } public string sex { get; set; } public int c_id { get; set; } } public bool IsReusable { get { return false; } } }
显示效果:
接着进行删除操作处理:
创建一个Del.ashx处理删除
public class Del : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; int id = int.Parse(context.Request["id"]); if(onDel(id)) { context.Response.Redirect("List.ashx"); } } bool onDel(int id) { string sql = string.Format("delete from stus where id={0}",id); return SqlHelper.ExeNonQuery(sql, CommandType.Text, null); } public bool IsReusable { get { return false; } } }
效果:
已经被删除了
再接下来进行查看的操作,着里要创建一个Dils.ashx和Dils.html
先看 Dils.html页面
<table border="1" cellspacing="0" cellpadding="0" width="300"> <tr> <th>编号</th> <th>姓名</th> <th>性别</th> <th>班级</th> </tr> <tr> <td>{id}</td> <td>{name}</td> <td>{sex}</td> <td>{c_id}</td> </tr> <tr> <td colspan="4"><a href="List.ashx">返回</a></td> </tr> </table>
然后Dils.ashx进行处理:
public class Dils : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; int id = int.Parse(context.Request["id"]); string strHtml = Commd.getFile("Dils.html"); DataTable dt=getTable(id); strHtml = strHtml.Replace("{id}", dt.Rows[0]["id"].ToString()); strHtml=strHtml.Replace("{name}",dt.Rows[0]["name"].ToString()); strHtml=strHtml.Replace("{sex}",dt.Rows[0]["sex"].ToString()); strHtml = strHtml.Replace("{c_id}", dt.Rows[0]["c_id"].ToString()); context.Response.Write(strHtml); } DataTable getTable(int id) { string sql = string.Format("select id,name,sex,c_id from stus where id={0}",id); DataTable dt = SqlHelper.GetTable(sql); return dt; } public bool IsReusable { get { return false; } } }
效果:
最后来看看修改的操作:
这里先建立一个Edit.html页面:
<form action="EditPros.ashx" method="post"> <table border="1" cellspacing="0" cellpadding="0" width="100%"> <tr> <th>编号</th> <th>姓名</th> <th>性别</th> <th>班级</th> </tr> <tr> <td><input type="txt" name="id" value="{id}" disabled="disabled" /> <input type="hidden" name="id" value="{id}" /> </td> <td><input type="txt" name="name" value="{name}" /></td> <td><input type="txt" name="sex" value="{sex}" /></td> <td><input type="txt" name="c_id" value="{c_id}" /></td> </tr> <tr> <td colspan="4"> <input type="submit" value="提交"/> <input type="reset" value="重置"/> </td> </tr> </table> </form>
然后创建Edit.ashx和 EditPros.ashx来进行处理
Edit.ashx处理跟Dils.ashx差不多
public class Dils : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; int id = int.Parse(context.Request["id"]); string strHtml = Commd.getFile("Dils.html"); DataTable dt=getTable(id); strHtml = strHtml.Replace("{id}", dt.Rows[0]["id"].ToString()); strHtml=strHtml.Replace("{name}",dt.Rows[0]["name"].ToString()); strHtml=strHtml.Replace("{sex}",dt.Rows[0]["sex"].ToString()); strHtml = strHtml.Replace("{c_id}", dt.Rows[0]["c_id"].ToString()); context.Response.Write(strHtml); } DataTable getTable(int id) { string sql = string.Format("select id,name,sex,c_id from stus where id={0}",id); DataTable dt = SqlHelper.GetTable(sql); return dt; } public bool IsReusable { get { return false; } } }
效果:
当点了提交后,要在 EditPros.ashx进行处理
public class EditPros : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; int id=int.Parse(context.Request["id"]); string name = context.Request["name"]; string sex = context.Request["sex"]; int c_id = int.Parse(context.Request["c_id"]); if (onUp(id, name, sex, c_id)) { context.Response.Redirect("List.ashx"); } } bool onUp(int id, string name, string sex, int c_id) { string sql = string.Format("update stus set name='{0}',sex='{1}',c_id={2} where id={3}", name, sex, c_id, id); return SqlHelper.ExeNonQuery(sql,CommandType.Text,null); } public bool IsReusable { get { return false; } } }
效果:
已经修改完成!
那么要是添加的话,怎么操作呢?
试试看。。。。。