创建ashx文档对OleDb数据库进行操作
2014-12-27 11:07
369 查看
<%@ WebHandler Language="C#" Class="data" %> //命名空间的声明 using System; using System.Web; using System.Data; using System.Data.OleDb; public class data : IHttpHandler{ public void ProcessRequest(HttpContext context){ //输出类型的声明 context.Response.ContentType = "text/plain"; //连接OleDb数据库 OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=E:\\study\\jq_ashx\\db1.mdb"); //接受前端页面发送来的参数 string add_content = context.Request.Form["info"]; string del_content = context.Request.Form["del_info"]; string search_content = context.Request.Form["search_info"]; string change_b_content = context.Request.Form["change_b"]; string change_a_content = context.Request.Form["change_a"]; //数据库的增删查改的sql语句 string sql_add = "insert into table1(information) values('" + add_content + "')"; string sql_del = "delete from table1 where information='" + del_content + "'"; string sql_search = "select information from table1"; string sql_change = "update table1 set information='" + change_a_content + "' where information='" + change_b_content + "'"; try{ connection.Open(); //插入 if(add_content != null){ OleDbCommand cmd_add = new OleDbCommand(sql_add,connection); cmd_add.ExecuteNonQuery(); } //删除 if(del_content != null){ OleDbCommand cmd_del = new OleDbCommand(sql_del,connection); cmd_del.ExecuteNonQuery(); } //查询 if(search_content != null){ OleDbCommand cmd_search = new OleDbCommand(sql_search,connection); OleDbDataReader reader = cmd_search.ExecuteReader(); string result = "no exist"; //默认不存在,如果在数据库查找到则会替换变量的值 while(reader.Read()){ string readerVal = reader.GetValue(0).ToString();//GetValue(i)从结果的第i+1个字段获取 if(search_content == readerVal){ result = "exist!"; break; } } context.Response.Write(result); } //替换 if(change_a_content != null & change_b_content != null){ OleDbCommand cmd_change = new OleDbCommand(sql_change,connection); cmd_change.ExecuteNonQuery(); } } catch (Exception exp){ context.Response.Write(exp.Message); } finally{ connection.Close(); } } public bool IsReusable { get { return false; } } }
总结:关于OleDbCommand类,增,改,替换操作,只需调用类里的ExecuteNonQuery()方法.而对于查询操作,则须声明OleDbDataReader下的一个参数,并调用类里的ExecuteReader()方法,最后利用循环将数据输出.
相关文章推荐
- SQL操作数据库,创建数据库,查看所有的数据库,对数据库进行增删改,查看当前使用的数据库
- 【MySQL 05】使用Java对MySQL进行操作(创建数据库)
- 利用ashx操作OleDb数据库返回json数据
- IOS 之使用FMDB进行SQLite数据库操作——表的创建与修改,以及数据的增删改查和多线程操作数据库
- 创建数据库并对数据库数据进行操作
- 郁闷,在ashx文件中不能进行数据库的UPDATE操作吗?
- 使用oledb对数据库进行增删改查及批量插入操作
- Hibernate动态建表,通过hbm.xml配置文件创建数据表,进行数据库操作, 动态模型
- Hibernate动态建表,通过hbm.xml配置文件创建数据表,进行数据库操作, 动态模型
- SQL 2008 R2 数据库操作,不允许保存更改。您所做的更改要求删除并重新创建一下表。您对无法重新创建的表进行了更改或启用了“阻止保存要求重新创建表的更改”选项
- sqlite数据库的基本操作基于FMDatabase(创建数据库,创建表,对标进行增,删,改,查)
- 创建sql server 2005 作业,进行异地数据库操作
- 单独使用MFC CRecord类进行数据库操作(原创心得)
- 怎样用XML技术对数据库进行操作
- VC中使用ADO进行数据库操作
- 如何进行跨数据库的数据操作?
- 利用SQLDMO进行数据库备份还原操作
- access数据库的一些少用操作,ASP,创建数据库文件,创建表,创建字段,ADOX
- 创建数据库链路参考文档