C# BETA2中操作ACCESS数据库(二)


所属类别:.NET

文章作者:未知

特别推荐:免费发布信息 承包关键词~~抢爆了!HOT!


C# BETA2中操作ACCESS数据库(二)上次说了如何在ADO。NET中执行“SELECT”语句,这次我们看看,如何执行“DELETE、UPDATE、INSERT”等语句。我们这次同样通过例子来看,其中我们用到了System.Data.OleDb.OleDbCommand类,其实,我们在前面执行SELECT的时候也用到了!下面我写出我的程序://修改留言本中特定的数据public Boolean UpdateNote(Notebook note){Boolean tempvalue=false;string sqlstr=""; //当时在这里定义,是为了在出现异常的时候看看我的SQL语句是否正确try{//用到了我前面写的那个得到数据库连接的函数OleDbConnection conn = getConn(); //getConn():得到连接对象,conn.Open();//确定我们需要执行的SQL语句,本处是UPDATE语句!sqlstr = "UPDATE notes SET ";sqlstr += "title='" + note.title + "',";sqlstr += "content='" + DealString(note.content) +"',";sqlstr += "author='" + note.author + "',";sqlstr += "email='" +note.email +"',";sqlstr += "http='" +note.http +"'";//sqlstr += "pic='" +note.pic +"'";sqlstr += " where id=" + note.id;//定义command对象,并执行相应的SQL语句OleDbCommand myCommand = new OleDbCommand(sqlstr,conn);myCommand.ExecuteNonQuery(); //执行SELECT的时候我们是用的ExecuteReader()conn.Close();//假如执行成功,则,返回TRUE,否则,返回FALSEtempvalue=true;return(tempvalue);}catch(Exception e){throw(new Exception("数据库更新出错:" + sqlstr + "\r" + e.Message)) ;}}这个例子是对于特定ID好的记录进行UPDATE操作,具体解释我都写在了程序中,其中的与数据库有关的语句是try内部的那些!其实,我们同样可以通过上面的那种模式执行INSERT、DELETE操作,下面我把我的程序列到下面!/*删除特定记录,通过string类型的ID删除字段,在我的程序中,我把这个函数重载了,这样我们就可以通过INT类型的ID参数来删除特定的字段了*/public Boolean DelNote(string delid){Boolean tempvalue=false;string sqlstr="";//连接数据库try{OleDbConnection conn = getConn(); //getConn():得到连接对象conn.Open();sqlstr = "delete * from notes where id=" + delid;//定义command对象,并执行相应的SQL语句OleDbCommand myCommand = new OleDbCommand(sqlstr,conn);myCommand.ExecuteNonQuery();conn.Close();//假如执行成功,则,返回TRUE,否则,返回FALSEtempvalue=true;return(tempvalue);}catch(Exception e){throw(new Exception("数据库更新出错:" + sqlstr + "\r" + e.Message)) ;}}细心的朋友们应该能看到,其实这个程序和上面的相比,只是哪个SQL语句不同而已,其他的都基本一样的!同样的,我们想在数据库中插入新的记录的时候也可以用这样的方式,程序如下://向留言本中添加数据public Boolean AddNote(Notebook note){Boolean tempvalue=false; //定义返回值,并设置初值//下面把note中的数据添加到数据库中!try{OleDbConnection conn = getConn(); //getConn():得到连接对象conn.Open();//设置SQL语句string insertstr="INSERT INTO notes(title, content, author, email, http, pic ,hits,posttime) VALUES ('";insertstr += note.title +"', '";insertstr += DealString(note.content) + "','";insertstr += note.author + "','";insertstr += note.email + "','";insertstr += note.http + "','";insertstr += note.pic + "',";insertstr += note.hits + ",'";insertstr += note.posttime +"')";OleDbCommand insertcmd = new OleDbCommand(insertstr,conn) ;insertcmd.ExecuteNonQuery() ;conn.Close();tempvalue=true;}catch(Exception e){throw(new Exception("数据库出错:" + e.Message)) ;}return(tempvalue);}//处理数据,在把数据存到数据库前,先屏蔽那些危险字符!public string DealString(string str){str=str.Replace("<","<");str=str.Replace(">",">");str=str.Replace("\r","");str=str.Replace("\'","’");str=str.Replace("\x0020","");return(str);}//恢复数据:把数据库中的数据,还原成未处理前的样子public string UnDealString(string str){str=str.Replace("<","<");str=str.Replace(">",">");str=str.Replace("","\r");str=str.Replace("’","\'");str=str.Replace("","\x0020");return(str);}我同时列出了两个函数UnDealString()和DealString( ),他们是对与输入内容做一些事先的处理和还原工作的!这几个程序因为都比较简单,所以我就不多说了!其实,我这样的对数据库操作也只是ADO。NET中的一部分,而通过DataSet来操作我现在还没有仔细研究过,所以我也不能写出什么东西来,以后的这几天我就准备好好看看那个东西了,到时候,我还会把我的感受写出来和大家分享!在补充一下,我前面用到的程序都是我在写一个留言本的测试程序时候用到的!如果有朋友有兴趣的话,我将贴出我的全部学习代码!好了,我要开始我的事情了!下次再见! 关闭本页

相关信息

·  局域网中代理服务器、路由器的配置方案

·  实力派还是粉丝团,博客比拼见分晓[Linux学习月主题活动]

· Linux 2.6.19.x 内核编译配置选项简介

· 用PERL实现一个简单的NIDS








....

91554 96194