Asp元件中級入門與精通系列之七

2022-03-02 04:45:54 字數 1833 閱讀 6997

開始資料庫操作。

常見的元件封裝

1.?

把資料庫的連線資訊封裝起來。

1>????

直接返回資料庫連線串,如,元件中

public? function? datasource()? as? variant

datasource? =? "driver=;server=yang;uid=sa;pwd=;? database=northwind"

end? function

?asp呼叫

set obj=server.createobject("webdb.getinfo")??

oconn=obj.datasource()

這樣的缺點是很明顯的,在asp檔案中,直接response.write oconn即可顯示出資料庫連線串,並沒有起到預期的作用。

?2>????

返回adodb.connection物件

public function getconn() as adodb.connection

set conn = new adodb.connection

conn.connectionstring = "provider=sqloledb.1;persist security info=false;user id=sa;password=;initial catalog=northwind;data source=yang"

conn.open

set getconn = conn

end function

?asp呼叫

dim dataquery

set dataquery=server.createobject("webdbtest.getinfomation")

set rs=server.createobject("adodb.recordset")

?sql="select * from employees"

rs.open sql,dataquery.getconn,1,3???

? response.write rs("lastname")

response.write dataquery.getconn.connectionstring

set rs=nothing

?這樣看起來不錯,只是response.write dataquery.getconn.connectionstring還是會顯示出資料庫連線串,大家可以測試。??

?2.?

將元件封裝到記錄集

可以看一下前段時間寫的

這段**不好的一點就是資料庫的連線放到了頁面中判斷,連線成功後,才開始訪問資料,看

<%

flag=dataquery.getconn()

if flag=false then

? response.write "資料庫沒有鏈結,請檢查"

? response.end

end if

set rs=dataquery.getemployeelist()

……%>??

個人認為,最好的做法是:

封裝到記錄集,元件方法中連線資料庫,操作完後,及時關閉

盡量在元件中生成html**,做到全部封裝。如下面的這種方式

<%

『list方法中包含了頁面的生成,連線的處理,分頁處理等等

??? dim objloop

??? set objloop=server.createobject("webdbtest.getinfomation ")

??? objloop.list()

??? set objloop=nothing

%>

而不是部分的封裝。

Asp元件中級入門與精通系列之七

開始資料庫操作。常見的元件封裝 1 把資料庫的連線資訊封裝起來。1 直接返回資料庫連線串,如,元件中 public?function?datasource as?variant datasource?driver server yang uid sa pwd database northwind e...

Asp元件中級入門與精通系列之七

開始資料庫操作。常見的元件封裝 1 把資料庫的連線資訊封裝起來。1 直接返回資料庫連線串,如,元件中 public?function?datasource as?variant datasource?driver server yang uid sa pwd database northwind e...

Asp元件中級入門與精通系列之七

開始資料庫操作。常見的元件封裝 1 把資料庫的連線資訊封裝起來。1 直接返回資料庫連線串,如,元件中 public?function?datasource as?variant datasource?driver server yang uid sa pwd database northwind e...