beta1中連接字符串主要需聲明數(shù)據(jù)庫(kù)的odbc的dns名字,但beta 2中的ole db連接的字符串就較復(fù)雜了,使用的類也不相同。由于db2等數(shù)據(jù)庫(kù)在大型機(jī)等應(yīng)用中使用非常廣泛,.net自然會(huì)全力支持此類編程開發(fā),為此,下文將逐步闡述ole db的db2開發(fā)。
ole db for db2驅(qū)動(dòng)
首先,visual studio.net和windows 2000 server中并未直接提供db2的ole db驅(qū)動(dòng),因此需下載microsoft host integration server 2000軟件,該軟件包中含ole db provider and odbc driver for db2,需下載server端軟件包,并在安裝時(shí)選擇安裝data integration,安裝完成后就可使用其ole db的db2驅(qū)動(dòng)了。
設(shè)置數(shù)據(jù)庫(kù)連接
首先,我們需配置好db2的客戶端設(shè)置,打開視圖選單項(xiàng)中的服務(wù)器資源管理器,在其數(shù)據(jù)連接中新增一個(gè)連接,指定程序選項(xiàng)時(shí)選擇microsoft ole db provider for db2,下一步連接選項(xiàng)中,填寫如下信息:
data source: qcdb(請(qǐng)酌情修改)
network:tcp/ip
ip address:數(shù)據(jù)庫(kù)服務(wù)器地址
network port:db2服務(wù)端口
username:數(shù)據(jù)庫(kù)訪問權(quán)限的用戶名
password:該用戶口令
database initial catalog:qcdb
package collection:qcdb
default schema:userid
下一步高級(jí)選項(xiàng)中選各自國(guó)家語(yǔ)言對(duì)應(yīng)的code page及ccsid,最后按確認(rèn)鍵完成創(chuàng)建連接。
假設(shè)qcdb這個(gè)db2數(shù)據(jù)庫(kù)中有一名為address的新數(shù)據(jù)表,其包含四個(gè)字段:name、email、age和address,我們可用服務(wù)器資源管理器打開上述連接及該數(shù)據(jù)表,可輸入數(shù)條中文記錄,確定中文字符能否正常顯示。
為項(xiàng)目增加數(shù)據(jù)庫(kù)連接
用visual studio新建一個(gè)asp.net項(xiàng)目,激活服務(wù)器資源管理器的上述連接,用鼠標(biāo)將該連接拖至新建的web form上,則ole db需要的連接字符參數(shù)全部自動(dòng)寫入web form程序中,上述連接生成的語(yǔ)句如下:
this.oledbconnection1.connectionstring = @"provider=db2oledb;
cache authentication=false;
integrated security="""";
password=freebsd9;
persist security info=true;
user id=userid;initial catalog=qcdb;
data source=qcdb;mode=readwrite;
extended properties="""";
appc remote lu alias="""";
appc local lu alias="""";
appc mode name=qpcsupp;
network transport library=tcpip;
host ccsid=37;pc code page=1282;
network address=172.23.169.1;
network port=4444;package collection=qcdb;
default schema=userid;alternate tp name="""";
process binary as character=false;
units of work=ruw"
用服務(wù)器資源管理器工具生成連接可避免錯(cuò)寫各種參數(shù),從而可快速地實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接。
web form顯示數(shù)據(jù)庫(kù)數(shù)據(jù)
為web form新增一個(gè)oledbdataadapter類的對(duì)象oledbdataadapter1,為其選擇數(shù)據(jù)連接時(shí)選上面建立的連接如:qcdb.qcdb.userid,選擇使用sql語(yǔ)句,生成sql語(yǔ)句如:select from address,最后提示完成。再為web form新增一個(gè)dataset并命名為dataset1用來放查詢得到的數(shù)據(jù)。新增一個(gè)顯示數(shù)據(jù)用的datagrid命名為datagird1,最后在web form的程序段中的page_init部分中增加如下代碼:
oledbconnection1.open();
//打開數(shù)據(jù)庫(kù)連接
oledbdataadapter1.fill(dataset1,"address");
//將得來的數(shù)據(jù)填入dataset
datagrid1.databind();
//綁定數(shù)據(jù)
oledbconnection1.close();
//關(guān)閉連接
編譯運(yùn)行后,可見address表中內(nèi)容被顯示于web form的databrid中。oledbconnection1等屬性設(shè)置都在生成的web form設(shè)計(jì)代碼中。
增加數(shù)據(jù)庫(kù)數(shù)據(jù)
在web form上新增對(duì)應(yīng)字段數(shù)量個(gè)數(shù)的textbox,及一個(gè)button,為該按鍵增加click響應(yīng)事件代碼如下:
this.oledbinsertcommand1.commandtext = "insert into address(name, email, age, address) values (’"+textbox1.text+"’,’"+textbox2.text+"’,’"+textbox3.text+"’,’"+textbox4.text+"’)";
oledbinsertcommand1.connection.open();
//打開連接
oledbinsertcommand1.executenonquery();
//執(zhí)行該sql語(yǔ)句
oledbinsertcommand1.connection.close();
//關(guān)閉連接
實(shí)際編程中,我們需注意sql語(yǔ)句的單引號(hào)問題以及數(shù)字和字符串處理問題。
刪除數(shù)據(jù)庫(kù)數(shù)據(jù)
在web form上新增一個(gè)textbox5及一個(gè)按鍵,要執(zhí)行刪除時(shí),在textbox5中填入要?jiǎng)h除記錄的name字段的值,然后按該按鍵執(zhí)行刪除。該按鍵代碼如下:
system.data.oledb.oledbcommand oledeletecommand1 = new system.data.oledb.oledbcommand();
this.oledbdataadapter1.deletecommand = oledeletecommand1;
//聲明為oledb命令
oledeletecommand1.commandtext="delete from address where name=’"+textbox5.text+"’";
oledeletecommand1.connection = this.oledbconnection1;
//指明連接
oledeletecommand1.connection.open();
//打開連接
oledeletecommand1.executenonquery();
//執(zhí)行sql語(yǔ)句
oledeletecommand1.connection.close();
//關(guān)閉連接
對(duì)于增加、更新和刪除操作后的datagrid刷新,可執(zhí)行類似的select sql語(yǔ)句即可。
上述代碼可供各種非sql server數(shù)據(jù)庫(kù)編程參考和使用,微軟為此提供了如ole db provider for oracle、as/400和vsam等眾多驅(qū)動(dòng),ole db方式確實(shí)提供了較odbc方式更為廣泛的數(shù)據(jù)存取范圍,如可存取access庫(kù)中的數(shù)據(jù)、郵件系統(tǒng)中的數(shù)據(jù)、web上的文本及圖形、目錄服務(wù)等等,符合odbc標(biāo)準(zhǔn)的數(shù)據(jù)源就是符合ole db標(biāo)準(zhǔn)的數(shù)據(jù)存儲(chǔ)的子集,而且ole db的api是符合com標(biāo)準(zhǔn)和基于對(duì)象的api,這些都是較原odbc方式有大幅改進(jìn),從而為綜合的數(shù)據(jù)集成處理提供了更廣泛的支持。
通過以上講解,筆者希望使讀者初步了解在visual studio.net中,以db2為后臺(tái)數(shù)據(jù)庫(kù)時(shí)應(yīng)用系統(tǒng)的開發(fā)步驟,也希望讀者舉一反三,領(lǐng)會(huì)思想和方法,以便更好地應(yīng)用到自己的系統(tǒng)開發(fā)中。
上述程序在中文windows 2000 server、ibm db2和visual studio.net beta 2環(huán)境中編譯并正常運(yùn)行。
新聞熱點(diǎn)
疑難解答