Class classes that connect to a database

Source: Internet
Author: User
Tags exit goto connect
Connection Database Option Explicit

' local variable (s) to hold property value (s)
Private lsserver as String ' local copy
Private Lsdatabase as String
' local variable (s) to hold property value (s)
Private lsusername as String ' local copy
Private Lspassword as String ' local copy
Private Lsselectsql as String ' local copy
' local variable (s) to hold property value (s)
Private lstable as String ' local copy

Public Property Let Table (ByVal vdata as String)
' used when assigning a value to the ' and on the left side of a assignment.
' syntax:x.table = 5
Lstable = Vdata
End Property


Public Property Get Table () as String
' used when retrieving value of a, on the right side of a assignment.
' Syntax:Debug.Print x.table
Table = lstable
End Property

Public Property Let Selectsql (ByVal vdata as String)
' used when assigning a value to the ' and on the left side of a assignment.
' Syntax:x.selectsql = 5
Lsselectsql = Vdata
End Property


Public Property Get Selectsql () as String
' used when retrieving value of a, on the right side of a assignment.
' Syntax:Debug.Print X.selectsql
Selectsql = Lsselectsql
End Property



Public Property Let Password (ByVal vdata as String)
' used when assigning a value to the ' and on the left side of a assignment.
' Syntax:x.password = 5
Lspassword = Vdata
End Property


Public Property Get Password () as String
' used when retrieving value of a, on the right side of a assignment.
' Syntax:Debug.Print X.password
Password = Lspassword
End Property



Public Property Let UserName (ByVal vdata as String)
' used when assigning a value to the ' and on the left side of a assignment.
' Syntax:x.username = 5
Lsusername = Vdata
End Property


Public Property Get UserName () as String
' used when retrieving value of a, on the right side of a assignment.
' Syntax:Debug.Print x.username
UserName = Lsusername
End Property



Public Property Let Server (ByVal vdata as String)
' used when assigning a value to the ' and on the left side of a assignment.
' Syntax:x.server = 5
Lsserver = Vdata
End Property


Public Property Get Server () as String
' used when retrieving value of a, on the right side of a assignment.
' Syntax:Debug.Print x.server
Server = Lsserver
End Property

Public Property Let Database (ByVal vdata as String)
' used when assigning a value to the ' and on the left side of a assignment.
' Syntax:x.database = 5
Lsdatabase = Vdata
End Property


Public Property Get Database () as String
' used when retrieving value of a, on the right side of a assignment.
' Syntax:Debug.Print x.database
Database = Lsdatabase
End Property


Public Function Connectdatabasebyodbcdns (Lsdns as String) as ADODB. Connection
On Error GoTo Errhandle
Dim Conn as New ADODB. Connection
Dim strconn as String
strconn = "dsn=" & Lsdns & _
"; Uid= "& Lsusername & _
"; Pwd= "& Lspassword
If conn.state = adStateOpen Then
Conn.close
End If
Conn.Open strconn
Conn.cursorlocation = adUseClient
Set Connectdatabasebyodbcdns = Conn
Exit Function
Errhandle:
Err.Raise 102, "Dbconn.cls", "Can ' t Connect to" & Database & "database!"
End Function
Public Function Connectdatabasebyodbc () as ADODB. Connection
On Error GoTo Errhandle
Dim Conn as New ADODB. Connection
Dim strconn as String
strconn = "Provider=msdasql.1" & _
"; User id= "& Lsusername & _
"; Password= "& Lspassword & _
"; Persist Security Info=false "& _
"; Initial catalog=" & Lsdatabase & _
";D ata source=" & Lsserver
If conn.state = adStateOpen Then
Conn.close
End If
Conn.Open strconn
Conn.cursorlocation = adUseClient
Set CONNECTDATABASEBYODBC = Conn
Exit Function
Errhandle:
Err.Raise 10



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.