貼篇文章,BETA2中ACCESS操作數據庫
2024-09-07 19:04:58
供稿:網友
朋友們好,回家已經有10天了,總算是開始學。net了,直接的感覺就是ms的幫助太差了,好多錯誤在上面,害的我走了好多彎路,結果好多東西還沒有完全搞好,簡直了!
由于beta2和beta1比較,變化太大了,而現在無論是書還是網絡上的資料基本都還停留在beta1上,是朋友們在學習的時候遇到好多問題還無處可查,這里我把我的學習過程中遇到的一些問題和體會拿出來與大家分享,希望能給也在學習過程中的朋友有些幫助!
我估計,朋友們在學習。net的過程中,遇到的最多的問題就是在和數據庫打交道的過程中了!所以這次我準備就從在beta2中如何操作數據庫開始了,數據庫采用我們最常用的access數據庫(其實是學習最常用的,實際中我更喜歡sql數據庫的)
在beta2中,。net提供了以下的namespace:
system.data namespace
system.data.oledb (和beta1中已經不同了,所以如果拿beta1中的程序到beta2中來運行肯定不可以的)
如果想講清楚這些東西,我不認為是我可以作到的,所以我想通過一些具體的程序來把我們對數據庫的最基本的操作(select、update、delete、insert等)演示一下,其他的還是需要朋友們在學習過程中來慢慢體會了!
要想操作一個數據庫,不論是那種操作,首先要做的肯定是打開數據庫,下面我們以access數據庫來做例子說明如何打開一個數據庫連接!在這里我們需要用到的是:system.data.oledb.oledbconnection類!(如果操作sql數據庫,我們最好使用system.data.sqlclient.sqlconnection類)
我先寫出我自己使用的程序:
using system.data
using system.data.oledb
public oledbconnection getconn()
{
string connstr="provider=microsoft.jet.oledb.4.0 ;data source=f://web//notesbook//class//leavenotes.mdb";
oledbconnection tempconn= new oledbconnection(connstr);
return(tempconn);
}
相信只要使用過ado的朋友應該都可以看懂的!我們先定義一個string類型的變量,其中存放了我們連接數據庫的連接字符串,然后在定義一個system.data.oledb.oledbconnection類型的對象并實例化,最后返回這個對象!需要說明一下的是,我并沒有把語句:tempconn.open();放到這個函數中,原因我我稍后在說明,這里只是先提醒一下!
通過上面的函數,我們就已經得到了類似于ado中的連接對象connection了!下面的就是具體操作數據庫了!
在具體講操作前,我認為有必要先認識一下下面的兩個類:
system.data.oledb.oledbdataadapter
system.data.oledb.oledbdatareader
system.data.oledb.oledbdataadapter:可以直接和dataset聯系,并操作數據源的,它的功能相對強大一些,因此也比較耗系統資源!
system.data.oledb.oledbdatareader:則有些類似于ado中的哪個只讀向前的記錄集,它最常用在只需要依次讀取并顯示數據的時候,相比system.data.oledb.oledbdataadapter來說,他耗用的系統資源要小!其實,oledbdatareader能實現的功能,oledbdataadapter都可以實現,不過從資源使用率的角度考慮我們應該盡量使用前者!但有些功能,卻是必須使用oledbdataadapter才可以實現的!
。select操作!
下面是我的自己在寫測試程序的時候用到了,先列出來看看oledbdatareader和oledbdataadapter是如何操作從數據庫中選擇記錄的:
//通過id得到當前留言詳細內容.通過string類型參數
public notebook getnotefromid(string noteid)
{
notebook tempnote=new notebook(); //定義返回值
try
{
oledbconnection conn = getconn(); //getconn():得到連接對象
string strcom = "select * from notes where id=" + noteid ;
oledbcommand mycommand =new oledbcommand(strcom,conn);
conn.open();
oledbdatareader reader;
reader =mycommand.executereader() ; //執行command并得到相應的datareader
//下面把得到的值賦給tempnote對象
if(reader.read())
{
tempnote.id=(int)reader["id"];
tempnote.title=reader["title"].tostring();
tempnote.content=reader["content"].tostring();
tempnote.author=reader["author"].tostring();
tempnote.email=reader["email"].tostring();
tempnote.http=reader["http"].tostring();
tempnote.pic=reader["pic"].tostring();
tempnote.hits=(int)reader["hits"];
tempnote.posttime=(datetime)reader["posttime"];
}
else //如沒有該記錄,則拋出一個錯誤!
{
throw(new exception("當前沒有該記錄!"));
}
reader.close();
conn.close();
}
catch(exception e)
{
//throw(new exception("數據庫出錯:" + e.message)) ;
}
return(tempnote); //返回databook對象
}
上面的程序就是通過oledbdatareader來得到特定的記錄的!其中用到的語句我單獨寫到下面:
oledbconnection conn = getconn(); //getconn():得到連接對象
string strcom = "select * from notes where id=" + noteid ; //sql語句
oledbcommand mycommand =new oledbcommand(strcom,conn); //建立oledbcommand對象
conn.open(); //注意我在前面說的open語句在這里使用到了!
oledbdatareader reader;
reader =mycommand.executereader() ; //執行command并得到相應的結果
我在每句話后都加入了說明,其中oledbconnection conn = getconn();就是通過我前面提到的getconn函數來得到數據庫連接的,其他語句沒有什么好說的,都很簡單,就不多說了!
我再列一個通過oledbdataadapter來得到記錄的例程:
//getlist():得到當前需要的留言列表
public dataview getnotelist()
{
dataview dataview;
system.data.dataset mydataset; //定義dataset
try
{
oledbconnection conn = getconn(); //getconn():得到連接對象
oledbdataadapter adapter = new oledbdataadapter();
string sqlstr="select * from notes order by posttime desc";
mydataset= new system.data.dataset();
adapter.selectcommand = new oledbcommand(sqlstr, conn);
adapter.fill(mydataset,"notes");
conn.close();
}
catch(exception e)
{
throw(new exception("數據庫出錯:" + e.message)) ;
}
dataview = new dataview(mydataset.tables["notes"]);
return(dataview);
}
這個程序或許有些復雜,同樣的,我還是先把那些關鍵語句列出,并說明:
oledbconnection conn = getconn(); //通過函數getconn()得到連接對象
oledbdataadapter adapter = new oledbdataadapter(); //實例化oledbdataadapter對象
string sqlstr="select * from notes order by posttime desc"; //sql語句
mydataset= new system.data.dataset(); //由于oledbdataadapter需要和dataset結合使用,所以在這里定義了dataset對象,其實說oledbdataadapter復雜,其實就是因為dataset的緣故dataset有些類似于ado中的recordset 對象,但功能遠遠超過了它,而且它和數據庫是斷開的,并能存放多個記錄集!
adapter.selectcommand = new oledbcommand(sqlstr, conn); //設置命令為selectcommand類型的
adapter.fill(mydataset,"notes"); //執行,并將結果添加到mydataset中的”notes”表中
conn.close(); //關閉連接!
在對上面的程序加一些補充說明,由于getnotelista是得到一系列記錄,并通過控件datagrid來做分頁顯示的,所以我返回的是一個dataview類型的對象!
啊呀,太晚了,今天就到這里,我要休息了,其它幾個操作我在以后在說吧,呵呵,睡覺了!