程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 操作數據庫的一個簡單類

操作數據庫的一個簡單類

編輯:.NET實例教程

''數據庫連接
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,

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved