朋友們好,總算又有時間了,搞了兩天,頭都大了!不過,真的是,。net的東西太多了,簡直有一種眼花繚亂的感覺,看來還是需要靜下心來慢慢測試!我的學習觀點就是多寫程序,多練習,你可以不去記憶那些在資料或幫助上能查到的東西,(我就沒有去記,即使是名字空間如何寫我都是看幫助),但一定要理解了,而如何理解,最直接的方法就是通過程序來把那些東西直接展示在眼前!
好了,不多說了,接著上次的東西,我們繼續來看看,如何在。net中操作數據庫(數據庫采用access 2000,至于sql,我在以后有機會了在說吧,其實如果懂了,access的話,適當變變就可以操作sql數據庫了?。?br>
上次說了如何在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,否則,返回false
tempvalue=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,否則,返回false
tempvalue=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","<br>");
str=str.replace("/'","’");
str=str.replace("/x0020"," ");
return(str);
}
//恢復數據:把數據庫中的數據,還原成未處理前的樣子
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);
}
我同時列出了兩個函數undealstring()和dealstring( ),他們是對與輸入內容做一些事先的處理和還原工作的!
這幾個程序因為都比較簡單,所以我就不多說了!
其實,我這樣的對數據庫操作也只是ado。net中的一部分,而通過dataset來操作我現在還沒有仔細研究過,所以我也不能寫出什么東西來,以后的這幾天我就準備好好看看那個東西了,到時候,我還會把我的感受寫出來和大家分享!
在補充一下,我前面用到的程序都是我在寫一個留言本的測試程序時候用到的!如果有朋友有興趣的話,我將貼出我的全部學習代碼!
好了,我要開始我的事情了!下次再見!