C#操作Access數(shù)據(jù)庫之SELECT操作淺析
C#操作Access數(shù)據(jù)庫之SELECT操作是如何的呢,讓我們開始講解:
下面是我的自己在寫測(cè)試程序的時(shí)候用到了,先列出來看看OleDbDataReader和OleDbDataAdapter是如何操作從數(shù)據(jù)庫中選擇記錄的:
- //通過ID得到當(dāng)前留言詳細(xì)內(nèi)容.通過STRING類型參數(shù)
- public Notebook getNoteFromID(string noteid)
- {
- Notebook tempnote=new Notebook(); //定義返回值
- try //C#操作Access數(shù)據(jù)庫之SELECT操作
- {
- OleDbConnection conn = getConn();
- //getConn():得到連接對(duì)象
- string strCom = "Select * from notes where id=" + noteid ;
- OleDbCommand myCommand =new OleDbCommand(strCom,conn);
- conn.Open();
- OleDbDataReader reader;
- reader =myCommand.ExecuteReader() ;
- //執(zhí)行command并得到相應(yīng)的DataReader
- //下面把得到的值賦給tempnote對(duì)象
- if(reader.Read())
- { //C#操作Access數(shù)據(jù)庫之SELECT操作
- 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 //如沒有該記錄,則拋出一個(gè)錯(cuò)誤!
- {
- throw(new Exception("當(dāng)前沒有該記錄!"));
- }
- reader.Close();
- conn.Close();
- }
- catch(Exception e)
- {
- //throw(new Exception("數(shù)據(jù)庫出錯(cuò):" + e.Message)) ;
- }
- return(tempnote); //返回Databook對(duì)象
- } //C#操作Access數(shù)據(jù)庫之SELECT操作
上面的程序就是通過OleDbDataReader來得到特定的記錄的!其中用到的語句我單獨(dú)寫到下面:
- OleDbConnection conn = getConn();
- //getConn():得到連接對(duì)象
- string strCom = "Select * from notes where id=" + noteid ;
- //SQL語句
- OleDbCommand myCommand =new OleDbCommand(strCom,conn);
- //建立OleDbCommand對(duì)象
- conn.Open(); //注意我在前面說的Open語句在這里使用到了!
- OleDbDataReader reader;
- reader =myCommand.ExecuteReader() ;
- //執(zhí)行command并得到相應(yīng)的結(jié)果
我在每句話后都加入了說明,其中OleDbConnection conn = getConn();就是通過我前面提到的getConn函數(shù)來得到數(shù)據(jù)庫連接的,其他語句沒有什么好說的,都很簡單,就不多說了!
C#操作Access數(shù)據(jù)庫之SELECT操作:再列一個(gè)通過OleDbDataAdapter來得到記錄的例程:
- //Getlist():得到當(dāng)前需要的留言列表
- public DataView getNoteList()
- {
- DataView dataview;
- System.Data.DataSet mydataset; //定義DataSet
- try
- {
- OleDbConnection conn = getConn(); //getConn():得到連接對(duì)象
- 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();
- } //C#操作Access數(shù)據(jù)庫之SELECT操作
- catch(Exception e)
- {
- throw(new Exception("數(shù)據(jù)庫出錯(cuò):" + e.Message)) ;
- }
- dataview = new DataView(mydataset.Tables["notes"]);
- return(dataview);
- }
這個(gè)程序或許有些復(fù)雜,同樣的,我還是先把那些關(guān)鍵語句列出,并說明:
- OleDbConnection conn = getConn();
- //通過函數(shù)getConn()得到連接對(duì)象
- OleDbDataAdapter adapter = new OleDbDataAdapter();
- //實(shí)例化OleDbDataAdapter對(duì)象
- string sqlstr="select * from notes order by posttime desc";
- //SQL語句
- //C#操作Access數(shù)據(jù)庫之SELECT操作
- mydataset= new System.Data.DataSet();
- //由于OleDbDataAdapter需要和DataSet結(jié)合使用,所以在這里定義了DataSet對(duì)象,
- //其實(shí)說OleDbDataAdapter復(fù)雜,
- //其實(shí)就是因?yàn)镈ataSet的緣故DataSet有些類似于ADO中的recordset 對(duì)象,
- //但功能遠(yuǎn)遠(yuǎn)超過了它,而且它和數(shù)據(jù)庫是斷開的,并能存放多個(gè)記錄集!
- adapter.SelectCommand = new OleDbCommand(sqlstr, conn);
- //設(shè)置命令為SelectCommand類型的
- adapter.Fill(mydataset,"notes");
- //執(zhí)行,并將結(jié)果添加到mydataset中的”notes”表中
- conn.Close(); //關(guān)閉連接!
在對(duì)上面的程序加一些補(bǔ)充說明,由于getNoteLista是得到一系列記錄,并通過控件DataGrid來做分頁顯示的,所以我返回的是一個(gè)DataView類型的對(duì)象!
C#操作Access數(shù)據(jù)庫之SELECT操作的基本內(nèi)容就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#操作Access數(shù)據(jù)庫之SELECT操作有所幫助。
【編輯推薦】