朋友們好,總算又有時(shí)間了,搞了兩天,頭都大了!不過,真的是,。net的東西太多了,簡(jiǎn)直有一種眼花繚亂的感覺,看來還是需要靜下心來慢慢測(cè)試!我的學(xué)習(xí)觀點(diǎn)就是多寫程序,多練習(xí),你可以不去記憶那些在資料或幫助上能查到的東西,(我就沒有去記,即使是名字空間如何寫我都是看幫助),但一定要理解了,而如何理解,最直接的方法就是通過程序來把那些東西直接展示在眼前!
好了,不多說了,接著上次的東西,我們繼續(xù)來看看,如何在。net中操作數(shù)據(jù)庫(kù)(數(shù)據(jù)庫(kù)采用access 2000,至于sql,我在以后有機(jī)會(huì)了在說吧,其實(shí)如果懂了,access的話,適當(dāng)變變就可以操作sql數(shù)據(jù)庫(kù)了!)
上次說了如何在ado。net中執(zhí)行“select”語句,這次我們看看,如何執(zhí)行“delete、update、insert”等語句。
我們這次同樣通過例子來看,其中我們用到了system.data.oledb.oledbcommand類,其實(shí),我們?cè)谇懊鎴?zhí)行select的時(shí)候也用到了!
下面我寫出我的程序:
//修改留言本中特定的數(shù)據(jù)
public boolean updatenote(notebook note)
{
boolean tempvalue=false;
string sqlstr=""; //當(dāng)時(shí)在這里定義,是為了在出現(xiàn)異常的時(shí)候看看我的sql語句是否正確
try
{
//用到了我前面寫的那個(gè)得到數(shù)據(jù)庫(kù)連接的函數(shù)
oledbconnection conn = getconn(); //getconn():得到連接對(duì)象,
conn.open();
//確定我們需要執(zhí)行的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對(duì)象,并執(zhí)行相應(yīng)的sql語句
oledbcommand mycommand = new oledbcommand(sqlstr,conn);
mycommand.executenonquery(); //執(zhí)行select的時(shí)候我們是用的executereader()
conn.close();
//假如執(zhí)行成功,則,返回true,否則,返回false
tempvalue=true;
return(tempvalue);
}
catch(exception e)
{
throw(new exception("數(shù)據(jù)庫(kù)更新出錯(cuò):" + sqlstr + "/r" + e.message)) ;
}
}
這個(gè)例子是對(duì)于特定id好的記錄進(jìn)行update操作,具體解釋我都寫在了程序中,其中的與數(shù)據(jù)庫(kù)有關(guān)的語句是try內(nèi)部的那些!
其實(shí),我們同樣可以通過上面的那種模式執(zhí)行insert、delete操作,下面我把我的程序列到下面!
/*刪除特定記錄,通過string類型的id刪除字段,在我的程序中,我把這個(gè)函數(shù)重載了,這樣我們就可以通過int類型的id參數(shù)來刪除特定的字段了*/
public boolean delnote(string delid)
{
boolean tempvalue=false;
string sqlstr="";
//連接數(shù)據(jù)庫(kù)
try
{
oledbconnection conn = getconn(); //getconn():得到連接對(duì)象
conn.open();
sqlstr = "delete * from notes where id=" + delid;
//定義command對(duì)象,并執(zhí)行相應(yīng)的sql語句
oledbcommand mycommand = new oledbcommand(sqlstr,conn);
mycommand.executenonquery();
conn.close();
//假如執(zhí)行成功,則,返回true,否則,返回false
tempvalue=true;
return(tempvalue);
}
catch(exception e)
{
throw(new exception("數(shù)據(jù)庫(kù)更新出錯(cuò):" + sqlstr + "/r" + e.message)) ;
}
}
細(xì)心的朋友們應(yīng)該能看到,其實(shí)這個(gè)程序和上面的相比,只是哪個(gè)sql語句不同而已,其他的都基本一樣的!同樣的,我們想在數(shù)據(jù)庫(kù)中插入新的記錄的時(shí)候也可以用這樣的方式,程序如下:
//向留言本中添加數(shù)據(jù)
public boolean addnote(notebook note)
{
boolean tempvalue=false; //定義返回值,并設(shè)置初值
//下面把note中的數(shù)據(jù)添加到數(shù)據(jù)庫(kù)中!
try{
oledbconnection conn = getconn(); //getconn():得到連接對(duì)象
conn.open();
//設(shè)置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("數(shù)據(jù)庫(kù)出錯(cuò):" + e.message)) ;
}
return(tempvalue);
}
//處理數(shù)據(jù),在把數(shù)據(jù)存到數(shù)據(jù)庫(kù)前,先屏蔽那些危險(xiǎn)字符!
public string dealstring(string str)
{
str=str.replace("<","<");
str=str.replace(">",">");
str=str.replace("/r","<br>");
str=str.replace("/'","’");
str=str.replace("/x0020"," ");
return(str);
}
//恢復(fù)數(shù)據(jù):把數(shù)據(jù)庫(kù)中的數(shù)據(jù),還原成未處理前的樣子
public string undealstring(string str)
{
str=str.replace("<","<");
str=str.replace(">",">");
str=str.replace("<br>","/r");
str=str.replace("’","/'");
str=str.replace(" ","/x0020");
return(str);
}
我同時(shí)列出了兩個(gè)函數(shù)undealstring()和dealstring( ),他們是對(duì)與輸入內(nèi)容做一些事先的處理和還原工作的!
這幾個(gè)程序因?yàn)槎急容^簡(jiǎn)單,所以我就不多說了!
其實(shí),我這樣的對(duì)數(shù)據(jù)庫(kù)操作也只是ado。net中的一部分,而通過dataset來操作我現(xiàn)在還沒有仔細(xì)研究過,所以我也不能寫出什么東西來,以后的這幾天我就準(zhǔn)備好好看看那個(gè)東西了,到時(shí)候,我還會(huì)把我的感受寫出來和大家分享!
在補(bǔ)充一下,我前面用到的程序都是我在寫一個(gè)留言本的測(cè)試程序時(shí)候用到的!如果有朋友有興趣的話,我將貼出我的全部學(xué)習(xí)代碼!
好了,我要開始我的事情了!下次再見!