Private dbDataBase As DAO.Database '数据库对象 '新建一个数据库,cDataBase(数据库的路径,数据库的密码(可选,默认空)) Public Function cDataBase(ByVal PathFile As String, Optional ByVal Password As String = "") As Boolean On Error Resume Next Set dbDataBase = CreateDatabase(PathFile, dbLangGeneral, dbEncrypt) dbDataBase.NewPassword "", Password Set dbDataBase = Nothing cDataBase = (Err.Number = 0) End Function
'---------------------------------------------------------------------- '数据库服务器 '-----------------------------------------------------------------------Public ServerName As String '服务器名 Public DataBaseName As String '数据库名 Public LogInID As String '登录名 Public Password As String '密码 Public TimeOut As Long '超时 Public Connect As String '连接数据库的网络名Private cnn As ADODB.Connection Public strMsg As String '查询返回信息'************************************************************************* '**函 数 名:ConnectString '**输 入:无 '**输 出:(String) -连接字符串 '**功能描述: '**全局变量: '**调用模块: '**作 者:马兆林 '**日 期:2004-11-29 21:23:28 '**修 改 人: '**日 期: '**版 本:V1.0.0 '************************************************************************* Public Function ConnectString() As StringIf Password = "" Then
'新建一个数据库,cDataBase(数据库的路径,数据库的密码(可选,默认空))
Public Function cDataBase(ByVal PathFile As String, Optional ByVal Password As String = "") As Boolean
On Error Resume Next
Set dbDataBase = CreateDatabase(PathFile, dbLangGeneral, dbEncrypt)
dbDataBase.NewPassword "", Password
Set dbDataBase = Nothing
cDataBase = (Err.Number = 0)
End Function
'数据库服务器
'-----------------------------------------------------------------------Public ServerName As String '服务器名
Public DataBaseName As String '数据库名
Public LogInID As String '登录名
Public Password As String '密码
Public TimeOut As Long '超时
Public Connect As String '连接数据库的网络名Private cnn As ADODB.Connection
Public strMsg As String '查询返回信息'*************************************************************************
'**函 数 名:ConnectString
'**输 入:无
'**输 出:(String) -连接字符串
'**功能描述:
'**全局变量:
'**调用模块:
'**作 者:马兆林
'**日 期:2004-11-29 21:23:28
'**修 改 人:
'**日 期:
'**版 本:V1.0.0
'*************************************************************************
Public Function ConnectString() As StringIf Password = "" Then
MsgBox "" & LogInID & " 登录密码为空!" ConnectString = "Provider=SQLOLEDB.1;Initial Catalog= " & DataBaseName & " ;Data Source=" & ServerName & ";UID=" & LogInID & ";PWD="
Else
MsgBox "" & LogInID & " 登录密码不为空!"
ConnectString = "Provider=SQLOLEDB.1;Initial Catalog= " & DataBaseName & " ;Data Source=" & ServerName & ";UID=" & LogInID & ";PWD=" & Password & "" End If
End Function'*************************************************************************
'**函 数 名:ExecuteSQL
'**输 入:ByVal SQL(String) -SQL查询字符串
'** :MsgString(String) -返回查询消息
'**输 出:(ADODB.Recordset) -输出查询记录
'**功能描述:执行SQL并且返回记录集
'**全局变量:
'**调用模块:
'**说 明:引用 Microsoft ActiveX Data Objects 2.7 Library
'**作 者:马兆林
'**日 期:2004-11-29 21:24:25
'**修 改 人:
'**日 期:
'**版 本:V1.0.0
'*************************************************************************
Public Function ExecuteSQL(ByVal SQL _
As String, _
MsgString As String) _
As ADODB.Recordset
'executes SQL and returns Recordset
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL) Set cnn = New ADODB.Connection cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE,EXECUTE", _
UCase$(sTokens(0))) Then cnn.Execute (SQL) MsgString = sTokens(0) & _
" query successful"
Else Set rst = New ADODB.Recordset rst.Open Trim$(SQL), cnn, _
adOpenKeyset, _
adLockOptimistic
'rst.MoveLast 'get RecordCount
Set ExecuteSQL = rst MsgString = "查询到" & rst.RecordCount & _
" 条记录 "
End If
ExecuteSQL_Exit: Set rst = Nothing Set cnn = Nothing Exit FunctionExecuteSQL_Error: MsgString = "查询错误: " & _
Err.Description MsgBox "" & MsgString & "", vbCritical + vbOKOnly, "信息"
Resume ExecuteSQL_ExitEnd Function
Public Property Get MsgString() As String MsgString = strMsgEnd Property