(原創)SQL和ACCESS資料庫連接管理類

來源:互聯網
上載者:User

今天無事.隨便寫了這個類.這個類很簡單,但也很實用,以後只要直接包含這個類檔案進需要的檔案去然後就可以隨時更改資料庫的類型了.

<%
'==============================================================
'類名:DBaseClass
'用途:可以根據使用者參數返回SQL或ACCESS的Connection對象
'用法:
'(Access資料庫)
' set dbclass=new DBaseClass 
' dbclass.dbtype="Access"     設定為Access資料庫類型 (預設為Access資料庫類型)
' dbclass.dbpath="data/data.mdb"  設定Access資料庫檔案地址
' set rs=dbclass.getrs("select * from [user]")  返回執行SQL語句後的rs對象
' set conn=dbclass.getconn()       返回conn對象
' set rs=dbclass.getbasers()       返回空的Recordset對象
'(SQL資料庫)
' dbclass.dbtype="sql"        設定為SQL資料庫  (預設為Access資料庫類型)
' dbclass.sqlusername="test"    設定登入資料庫的使用者名稱(預設是sa使用者)
' dbclass.sqlpassword="1010"    設定登入資料庫的使用者密碼(預設為空白)
' dbclass.sqldatabasename="user"設定SQL資料庫的庫名
' dbclass.sqllocalname="192.168.0.1" 設定SQL資料庫的伺服器位址(預設為本機地址)
' set rs=dbclass.getrs("select * from [user]")  返回執行SQL語句後的rs對象
' set conn=dbclass.getconn()       返回conn對象
' set rs=dbclass.getbasers()       返回空的Recordset對象
' set dbclass=nothing              關閉對象
'================================================================
Class DBaseClass
'資料庫驅動程式連接字串變數
Dim D_ConnStr
'Access資料庫檔案的路徑
Dim D_dbPath
'SQL資料庫的設定變數
Dim D_SqlDatabaseName,D_SqlPassword,D_SqlUsername,D_SqlLocalName
'Conn對象
Dim D_Conn,D_Rs
'資料庫驅動類型
'值:"1","A" "a" "Access" "access" 則為Access資料庫驅動連線物件
'值:"2","S" "s" "SQL" "sql" 則為SQL資料庫驅動連線物件
Dim D_DbType
'======================================
'函數名:被始化類
'======================================
Private Sub Class_Initialize
  '預設是Access資料庫
  D_DbType="Access"
  D_SqlUsername="sa"
  D_SqlPassword=""
  D_SqlLocalName="(local)"
End Sub

'======================================
'函數名:登出類
'======================================
Private Sub Class_Terminate
on error resume next
 if isObject(D_Rs) then
   D_Rs.close
   set D_Rs=nothing
 end if
 if isObject(D_Conn) then
   D_Conn.close
   set D_Conn=nothing
 end if
End Sub

Public Property Let dbType(bNewValue)
 Select case lcase(cstr(bNewValue))
   case "1","a","access"
      D_DbType = "Access"
   case "2","s","sql"
      D_DbType="SQL"
   case else
      D_DbType="Access"
 End select
End Property
Public Property Get dbType
 dbType =D_DbType
End Property

'ACCESS資料連線的幾個屬性變數的賦值操作
Public Property Let dbPath(bNewValue)
 D_dbPath = bNewValue
End Property
Public Property Get dbPath
 dbPath =D_dbPath
End Property

'SQL資料連線的幾個屬性變數的賦值操作
Public Property Let SqlDatabaseName(bNewValue)
 D_SqlDatabaseName = bNewValue
End Property
Public Property Get SqlDatabaseName
 SqlDatabaseName =D_SqlDatabaseName
End Property

Public Property Let SqlPassword(bNewValue)
 D_SqlPassword = bNewValue
End Property
Public Property Get SqlPassword
 SqlPassword =D_SqlPassword
End Property

Public Property Let SqlUsername(bNewValue)
 D_SqlUsername = bNewValue
End Property
Public Property Get SqlUsername
 SqlUsername =D_SqlUsername
End Property

Public Property Let SqlLocalName(bNewValue)
 D_SqlLocalName = bNewValue
End Property
Public Property Get SqlLocalName
 SqlLocalName =D_SqlLocalName
End Property

'==============================================
'函數功能:擷取資料庫Connection對象
'說明:
'傳回值:資料庫Connection對象
'==============================================
Public Function getConn()
on error resume next
 if isObject(D_Conn) and not isNull(D_Conn) then
    getConn=D_Conn
 exit function
 end if
 Set D_Conn= Server.CreateObject("ADODB.Connection")
 D_Conn.Open getConnStr()
 if err.number>0 then
   err.clear
   set D_Conn=nothing
   getConn=null
   exit function
 end if
 set getConn=D_Conn
End Function

'==============================================
'函數功能:擷取資料庫RecordSet對象
'說明:此函數獲得的RecordSet對象同Connection對象的execute執行方法一樣
'參數:SqlStr  執行的SQL語言
'傳回值:資料庫RecordSet對象
'==============================================
Public Function getRs(SqlStr)
if SqlStr="" then
 getRs=null
 exit function
end if
Set getRs=getConn().execute(SqlStr)
End Function

'==============================================
'函數功能:擷取資料庫RecordSet對象
'說明:此函數獲得的RecordSet對象建立RecordSet對象一樣
'傳回值:資料庫RecordSet對象
'==============================================
Public Function getBaseRs()
 if isObject(D_Rs) and not isNull(D_Rs) then
   set getDbRs=D_Rs
   exit function
 else
   set D_Rs = server.CreateObject("ADODB.Recordset")
 end if
 Set getDbRs=D_Rs
End Function

'==============================================
'函數功能:擷取資料庫連接驅動字串
'說明:此函數是根據D_dbType變數得到不到的字串
'傳回值:資料庫連接驅動字串
'==============================================
Private Function getConnStr()
  Select case D_DbType
      case "Access"
     getConnStr=getAccessConnStr()
   case "SQL"
        getConnStr=getSqlConnStr()
   case else
     getConnStr=""
  end Select
End Function

'==============================================
'函數功能:擷取Access資料庫連接驅動字串
'說明:此函數是根據D_DbPath變數得到字串
'傳回值:Access資料庫連接驅動字串
'==============================================
Private Function getAccessConnStr()
  if D_DbPath<>"" then
    getAccessConnStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(D_DbPath)
  else
    getAccessConnStr=""
  end if
End Function

'==============================================
'函數功能:擷取SQL資料庫連接驅動字串
'說明:此函數是根據D_SqlDatabaseName和D_SqlUsername和D_SqlPassword和D_SqlLocalName變數得到字串
'傳回值:SQL資料庫連接驅動字串
'==============================================
Private Function getSqlConnStr()
  if D_SqlDatabaseName<>"" then
    getSqlConnStr = "Provider = Sqloledb; User ID = " & D_SqlUsername & "; Password = " & D_SqlPassword & "; Initial Catalog = " & D_SqlDatabaseName & "; Data Source = " & D_SqlLocalName & ";"
  else
    getSqlConnStr=""
  end if
End Function

End Class
%>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.