'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'資料庫操作類別
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'名稱:Class_DBOperate
'版本:0.2
'作者:qihangnet
'更新:2005年6月14日
'作用:𢠃化資料庫操作的流程
'授權:免使用
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Class Class_DBOperate
'**變數定義**
'Conn: 資料庫連接物件
'Conn_Str:資料庫連結字串
Private Conn,Conn_Str
'**私有方法**
'初始化
Private Sub Class_Initialize()
Set Conn = Server.CreateObject("ADODB.Connection")
End Sub
'終止
Private Sub Class_Teriminate()
Set Conn = Nothing
End Sub
'**屬性**
'輸出資料庫連接字串
' 返回值類型:string
Property Get ConnectString
ConnectString = Conn_Str
End Property
'設置資料庫連接字串
' 參數:str --- string
Property Let ConnectString(str)
Conn_Str = str
End Property
'**公開方法 (事件)**
'開啟資料庫
Public Sub DB_Open()
Conn.ConnectionString = Conn_Str
Conn.Open
End Sub
'關閉資料庫
Public Sub DB_Close()
Conn.Close
End Sub
'資料庫查R(SQL䱇句)
' 參數及類別:sql ---- string
' 返回值類型:記錄集
' 前提:資料庫狀態為打開
Public Function DB_Select(sql)
Set DB_Select = Conn.Execute(sql)
End Function
'資料庫執行(SQL䱇句)
' 參數及類別:sql ---- string
' 返回值類型:整數
' 返回值含義:受影響行數
' 前提:資料庫狀態為打開
Public Function DB_Excute(sql)
Dim rs_affected
Conn.Execute sql,rs_affected
DB_Excute = rs_affected
End Function
End Class
'以下為使用範例
Dim rdb : Set rdb = New Class_DBOperate ' 初始化一個 rdb 物件,並呼叫 New Class_DBOperate 類別的 Class_Initialize 方法
rdb.ConnectString = "Provider=SQLOLEDB;driver={sql server};database=car;server=127.0.0.1;uid=sa;pwd=sapasswd"
rdb.DB_Open
Dim rst, sqlstr
sqlstr="select * from op"
set rst =rdb.DB_Select( sqlstr )
if not rst.eof then
while not rst.eof
response.write rst(0) & rst(1) & "<br />"
rst.movenext
wend
end if
rst.close
set rst=nothing
rdb.DB_Close
Set rdb = Nothing ' 呼叫 New Class_DBOperate 類別的呼叫 Class_Terminate 方法
'========================================================================
Function GetRs(SQL)
Dim Rs, Conn
Set Rs=Server.CreateObject("ADODB.Recordset")
Set Conn = GetCn()
Rs.Open SQL, Conn, 3
Set GetRs = Rs
Set Rs = Nothing
Set Conn = Nothing
End Function
Function GetCn()
Dim Conn
Set Conn=Server.CreateObject("ADODB.Connection")
Conn.Open GetConProfile()
Set GetCn = Conn
Set Conn = Nothing
End Function
Function GetConProfile()
Dim SQLServer, SQLLogin, SQLPwd, SQLDB
SQLServer="127.0.0.1"
SQLLogin="sa"
SQLPwd="sapasswd"
SQLDB="car"
GetConProfile = "Driver={SQL Server};Server=" & SQLServer & ";Uid=" & SQLLogin & ";Pwd=" & Trim(SQLPwd) & ";Database=" & SQLDB & ""
End Function
Function Execute(SQL)
Dim Cm, Conn, Errors
Set Cm=Server.CreateObject("ADODB.Command")
Set Conn = GetCn()
Cm.ActiveConnection = Conn
Cm.Prepared = True
Cm.CommandText = SQL
Cm.CommandType = 1
Cm.Execute
Set Errors = Conn.Errors
Execute = CStr(Conn.Errors.count)
Set Cm = Nothing
Set Conn = Nothing
End Function
Function GetCm()
Dim Cm, Conn
Set Cm=Server.CreateObject("ADODB.Command")
Set Conn = GetCn()
Cm.ActiveConnection = Conn
Set GetCm = Cm
End Function
'以下為使用範例
Dim rst, sqlstr
sqlstr="select * from op"
set rst = GetRs( sqlstr )
if not rst.eof then
while not rst.eof
response.write rst(0) & rst(1) & "<br />"
rst.movenext
wend
end if
If isobject("rst") Then
rst.close
End If
set rst=nothing吉公网安备 22020202000301号