vb 与SQL /ACCESS的连接 从 ini文件里读配置


Option Explicit
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
                            ByVal lpKeyName As Any, _
                            ByVal lpDefault As String, _
                            ByVal lpReturnedString As String, _
                            ByVal nSize As Long, _
                            ByVal lpFileName As String _
                           ) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
                              ByVal lpKeyName As Any, _
                              ByVal lpString As Any, _
                              ByVal lpFileName As String _
                             ) As Long
                             
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private StrServer As String
Private StrUid As String
Private StrPwd As String
Private StrDataBase As String
Private StrSQLDSN As String

Private StrDbPath As String
Private StrMDBDSN As String

Public Group_UserNow As New Group_UserType
Private iDbType As Integer
Public Function getStrSQLDSN() As String
    getStrSQLDSN = StrSQLDSN
End Function
Public Function getStrMDBDSN() As String
    getStrMDBDSN = StrMDBDSN
End Function
Public Function getStrDSN() As String
    Select Case iDbType
    Case 0
        getStrDSN = getStrSQLDSN
    Case 1
        getStrDSN = getStrMDBDSN
    End Select
End Function
Public Function setStrSQLDSN() As Boolean
    setStrSQLDSN = True
    StrServer = GetIniStr("SQLSERVER", "SERVER")
    StrDataBase = GetIniStr("SQLSERVER", "DATABASE")
    StrUid = GetIniStr("SQLSERVER", "UID")
    StrPwd = GetIniStr("SQLSERVER", "PWD")
On Error GoTo ERR_FLG
    StrSQLDSN = "  driver={SQL server}" & _
                "; server=" & StrServer & _
                "; uid=" & StrUid & _
                "; pwd=" & StrPwd & _
                "; database=" & StrDataBase
   Exit Function
ERR_FLG:
   setStrSQLDSN = False
End Function
Public Function setStrMDBDSN() As Boolean
    setStrMDBDSN = True
    StrDbPath = GetIniStr("ACCESS", "DBPATH")
   On Error GoTo ERR_FLG
   StrMDBDSN = " Provider=Microsoft.Jet.OLEDB.4.0" & _
                ";Data Source=" & StrDbPath
   Exit Function
ERR_FLG:
   setStrMDBDSN = False
End Function
Public Function setDbType() As Boolean
    setDbType = True
    iDbType = CStr(GetIniStr("DBTYPE", "TYPE"))
On Error GoTo ERR_FLG
   Exit Function
ERR_FLG:
   setDbType = False
End Function
Public Function GetIniTF(ByVal In_Key As String) As Boolean
On Error GoTo GetIniTFErr
GetIniTF = True
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString "Setting", In_Key, "", GetStr, 256, App.Path & "/SourceDB.ini"
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "1" Then
   GetIniTF = True
   GetStr = ""
Else
   GoTo GetIniTFErr
End If
Exit Function
GetIniTFErr:
   Err.Clear
   GetIniTF = False
   GetStr = ""
End Function
Public Function WriteIniTF(ByVal In_Key As String, ByVal In_Data As Boolean) As Boolean
On Error GoTo WriteIniTFErr
WriteIniTF = True
If In_Data = True Then
 WritePrivateProfileString "Setting", In_Key, "1", App.Path & "/COMMON/database.ini"
Else
 WritePrivateProfileString "Setting", In_Key, "0", App.Path & "/COMMON/database.ini"
End If
Exit Function
WriteIniTFErr:
   Err.Clear
   WriteIniTF = False
End Function
Public Function GetIniStr(ByVal AppName As String, ByVal In_Key As String) As String
On Error GoTo GetIniStrErr
If VBA.Trim(In_Key) = "" Then
   GoTo GetIniStrErr
End If
Dim GetStr As String
GetStr = VBA.String(128, 0)
 GetPrivateProfileString AppName, In_Key, "", GetStr, 256, App.Path & "/COMMON/database.ini"
  GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "" Then
   GoTo GetIniStrErr
Else
   GetIniStr = GetStr
   GetStr = ""
End If
Exit Function
GetIniStrErr:
   Err.Clear
   GetIniStr = ""
   GetStr = ""
End Function
Public Function WriteIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String) As Boolean
On Error GoTo WriteIniStrErr
WriteIniStr = True
If VBA.Trim(In_Data) = "" Or VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then
   GoTo WriteIniStrErr
Else
 WritePrivateProfileString AppName, In_Key, In_Data, App.Path & "/COMMON/database.ini"
End If
Exit Function
WriteIniStrErr:
   Err.Clear
   WriteIniStr = False
End Function
Public Sub Main()
If Not setDbType Then
    MsgBox "读取数据库配置类型选项失败!"
End If
If Not setStrSQLDSN Then
    MsgBox "读取SQL数据库配置选项失败!"
End If
If Not setStrMDBDSN Then
    MsgBox "读取ACCESS数据库配置选项失败!"
End If
If Not testConnection Then
  frmLogin.btnOK.Enabled = False
End If
frmSplash.Show
End Sub

配置文件 参数说明
[DBTYPE]
TYPE=0--------------------------------------------------------------------采用的数据库类型 0-SQL 1-ACCESS
[SQLSERVER]
TYPE=0--------------------------------------------------------------------SQL的数据库类型值
SERVER=zjc-------------------------------------------------------------SQL SERVER的Name
DATABASE=database-------------------------------------------------数据库Name
UID=sa--------------------------------------------------------------------登陆身份号
PWD=sa------------------------------------------------------------------登陆密码
[ACCESS]
TYPE=1-------------------------------------------------------------------ACCESS的数据库类型值
DBPATH=G:/工作/PROJECT/database/database.mdb-----ACCESS数据库文件的路径
Public conn As New ADODB.Connection Public nowconnectstring As String Public operatetype As Integer Public Declare Function GetPrivateProfileString Lib "Kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String, ByVal lpRetunedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long Public Declare Function WritePrivateProfileString Lib "Kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, lpKeyName As Any, ByVal lpAdd As String, ByVal lpFileName As String) As Boolean 'Public pzhm As Variant Public server As String Public loginname As String Public database As String Public pass As String Public path As String Private mSystemData As Object '帐套连接信息对象(帐套名,当前用户等) '=================================================================='定义公用变量 '在工程中新建一模块,并将以下代码加入到该的模块中(即module1.bas) Public tmpDataGrid As DataGrid '用与确定要实现滚动的DataGrid控件 Public tmpDataGridRowNum As Long '有多少行数据 Public Const GWL_WNDPROC = (-4) Public Const WM_COMMAND = &H111; Public Const WM_MBUTTONDOWN = &H207; Public Const WM_MBUTTONUP = &H208; Public Const WM_MOUSEWHEEL = &H20A; Public Oldwinproc As Long Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long Public Property Get SystemProfile() As Object If mSystemProfile Is Nothing Then Set mSystemProfile = GLData.SystemProfile End If Set SystemProfile = mSystemProfile End Property Public Property Set SystemProfile(ByVal NewVal As Object) Set mSystemProfile = NewVal Set GLData.SystemProfile
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值