''數據庫連接
Public conn As ADODB.Connection
''保持屬性值的局部變量
Private mvarconnstring As String ''局部復制
Public Property Let connstring(ByVal vData As String)
''向屬性指派值時使用,位於賦值語句的左邊。
''Syntax: X.connstring = 5
mvarconnstring = vData
End Property
Public Property Get connstring() As String
''檢索屬性值時使用,位於賦值語句的右邊。
''Syntax: Debug.Print X.connstring
connstring = mvarconnstring
End Property
''打開數據庫連接
Public Function dbOpen()
Set conn = New ADODB.Connection
conn.ConnectionString = connstring
conn.ConnectionTimeout = 30
conn.Open
End Function
''關閉數據庫連接
Public Function dbClose()
conn.Close
'' conn = Null
End Function
Public Function recordcount(ByVal sqlstring As String) As Integer
dbOpen
Set rs = New ADODB.Recordset
rs.Open sqlstring, conn, adOpenStatic, adLockReadOnly, adCmdText
recordcount = rs.recordcount
rs.Close
dbClose
End Function
***************************************************************************
實用實例
Public db As XMDB
Private Sub Command1_Click()
Dim user_name As String
Dim user_pwd As String
user_name = Trim(Text1.Text)
user_pwd = Trim(Text2.Text)
''MsgBox ("用戶名是:" + user_name + Chr(10) + "密碼是:" & user_pwd)
Dim int_count As Integer
int_count = db.recordcount("select * from tb_Operator where oper_name=''" & user_name & "'' And oper_pwd=''" & user_pwd & "''")
If int_count > 0 Then
MsgBox ("登陸成功!")
Else
MsgBox ("用戶名或密碼錯誤!")
End If
''Set conn = New ADODB.Connection
''conn.ConnectionString = "Driver={SQL Server};server=server;uid=sa;pwd=;database=dh_cms"
''conn.ConnectionTimeout = 30
''conn.Open
''Set rs = New ADODB.Recordset
''rs.Open "select * from tb_Operator where oper_name=''" & user_name & "'' And oper_pwd=''" & user_pwd & "''", conn, adOpenStatic,