如何用INI文件连接SQL数据库?数据库连接错误的时候显示提示!

解决方案 »

  1.   

    ini 只能保存信息吧?怎么连接数据库?不可能吧!单单ini文件什么都不是啊。
      

  2.   

    就是调用INI文件的内容!方便在改动IP的时候!直接改动INI文件下的就可以了!
      

  3.   

    那就放入INI文件中,在连接数据库时所用的参数从INI文件中读,读写INI文件的API如下
    Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias _
        "GetPrivateProfileIntA" (ByVal lpApplicationName As String, _
        ByVal lpKeyName As String, ByVal nDefault As Long, _
        ByVal lpFileName As String) As LongPublic 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 LongPublic 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
    Public Declare Function WritePrivateProfileSection Lib "kernel32" _
        Alias "WritePrivateProfileSectionA" _
        (ByVal lpAppName As String, ByVal lpString As String, _
        ByVal lpFileName As String) As Long
      

  4.   

    '******************************************************************************
    '**
    '** 模块:mdlIniFile
    '** 作用:操作 INI 文件
    '**
    '******************************************************************************Option ExplicitPrivate 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 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 LongPublic Function ReadINI(sSection As String, sKeyName As String, sINIFileName As String) As String
    On Error Resume Next    Dim sRet    As String
        Dim strTemp As String    sRet = String(255, Chr(0))
        strTemp = Left(sRet, GetPrivateProfileString(sSection, ByVal sKeyName, "", sRet, Len(sRet), sINIFileName))
        
        ReadINI = Replace(strTemp, Chr(0), "")
    End FunctionPublic Function WriteINI(sSection As String, sKeyName As String, sNewString As String, sINIFileName As String) As Boolean
    On Error Resume Next    Call WritePrivateProfileString(sSection, sKeyName, sNewString, sINIFileName)
        
        WriteINI = (Err.Number = 0)
    End Function
      

  5.   

    结合楼上的看法,在看这个。
    dim buff(6) As String
    If Dir(App.Path & "\defalue.ini", 2) = "" Then
      MsgBox "没有找到配置文件,请配置", 48, "错误"
      buff(0) = "127.0.0.1"
      buff(1) = "pubs"
      buff(2) = "sa"
      buff(3) = ""
      buff(4) = "pub_info"
      buff(5) = "1433"
    Else
    buff(0) = String(255, 0)
    ret = GetPrivateProfileString("Ip", "Address", "127.0.0.1", buff(0), 255, App.Path & "\defalue.ini") '获得IP地址
    'connstring = "Provider=sqloledb.1;Data Source=" & Left(buff(0), InStr(1, buff(0), Chr(0), vbBinaryCompare) - 1) & ";Network Library=DBMSSOCN;"
    connstring = "driver={SQL Server};server=" & Left(buff(0), InStr(1, buff(0), Chr(0), vbBinaryCompare) - 1)
    buff(5) = String(255, 0)
    ret = GetPrivateProfileString("Ip", "Poxty", "1433", buff(5), 255, App.Path & "\defalue.ini") '端口号
    connstring = connstring & "," & Left(buff(5), InStr(1, buff(5), Chr(0), vbBinaryCompare) - 1) & ";Network Library=DBMSSOCN;"
    buff(1) = String(255, 0)
    ret = GetPrivateProfileString("DataBase", "DataBaseName", "pubs", buff(1), 255, App.Path & "\defalue.ini") '数据库名
    connstring = connstring & "Initial Catalog=" & Left(buff(1), InStr(1, buff(1), Chr(0), vbBinaryCompare) - 1)
    buff(2) = String(255, 0)
    ret = GetPrivateProfileString("UserName", "User", "sa", buff(2), 255, App.Path & "\defalue.ini") '登陆用户名
    connstring = connstring & ";User Id=" & Left(buff(2), InStr(1, buff(2), Chr(0), vbBinaryCompare) - 1)
    buff(3) = String(255, 0)
    ret = GetPrivateProfileString("UserName", "Password", "windows", buff(3), 255, App.Path & "\defalue.ini") '密码
    connstring = connstring & ";Password=" & Left(buff(3), InStr(1, buff(3), Chr(0), vbBinaryCompare) - 1) & ";"
    buff(4) = String(255, 0)
    ret = GetPrivateProfileString("Fields", "Name", "pub_info", buff(4), 255, App.Path & "\defalue.ini") '查询字段名
    sql = "select  * from " & Left(buff(4), InStr(1, buff(4), Chr(0), vbBinaryCompare) - 1)大概就是这样了,我也没测试,是从我程序里直接粘贴过来的,哪不能用的话就自己该吧
      

  6.   

    上面贴的是读写INI的模块,下面举个例子,如何用ini存储SQL的连接。Public Sub CnnData()On Error GoTo ErrCode    Dim strCnn              As String
        Dim strPath             As String
        Dim strSqlServerName    As String
        Dim strUserName         As String
        Dim strUserPsw          As String
        
    ConnectSQL:    Set gcnnData = New ADODB.Connection
        strPath = App.Path & IIf(Right(App.Path, 1) = "\", "", "\")
                     
        strSqlServerName = ReadINI("SQL Server", "Server Name", strPath & "setting.ini")
        strUserName = ReadINI("SQL Server", "User Name", strPath & "setting.ini")
        strUserPsw = ReadINI("SQL Server", "User PassWord", strPath & "setting.ini")
        
        If Len(Trim$(strSqlServerName & vbNullString)) = 0 Then
           Err.Number = -2147467259
           GoTo ErrCode
        End If    strCnn = "Driver={SQL Server}" & _
                    ";Server=" & strSqlServerName & _
                    ";Uid=" & strUserName & _
                    ";Pwd=" & strUserPsw & _
                    ";Database=lybug"
                    
        With gcnnData
            .CommandTimeout = 0
            .ConnectionString = strCnn
            .CursorLocation = adUseClient
            .Open
        End WithErrCode:
        '/* If Connect Sql Server Error,Show The Connect Form */
        If Err.Number = -2147467259 Or Err.Number = -2147217843 Then
            FrmConnectSql.Show vbModal
            GoTo ConnectSQL
        End IfEnd Sub