我做的一个毕业设计的数据库是SQL2000,用的是ADO方法(没有用控件),每次换了机了后,都要重新设置连接字符串,这样很麻烦,并且打包后就改不了了,请问有没有方法,使生程序自动获得连接字符串。

解决方案 »

  1.   

    你将连接连接信息写入INI文件中,保存于System32目录下。每次连接如连接不成功时,自动弹出设置框,设置连接信息
      

  2.   

    注:建议的方法连接信息分项显示,如以下格式:[DatabaseInfo]
    Provider=SQLOLEDB.1 
    Data Source=AJIU   '服务器名
    Initial Catalog=TestData '连接数据库名
    User ID=sa
    password=
    Flag=1
      

  3.   

    INI文件的写法就同我上面所书,有两个API函数是用于读写INI文件的,如下:
    Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" _
                            (ByVal lpBuffer As String, ByVal nSize As Long) As LongPrivate 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 LongPrivate 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
                                     
    '********************************************************************************
    '** 函数功能:从ini配置文件中读取指定段名、关键字名的值
    '** 调用语法: GetInIKeyValue(SectionName as string,KeyName As String,FileName As String)
    '** 参数说明:
    '**         SectionName :段名
    '**         KeyName     :关键字名
    '**         FileName    :ini文件名包括路径
    '** 返 回 值:
    '**         String      :返回关键字值
    '** 处理说明:
    '**         调用API函数GetPrivateProfileString
    '******************************************************************************
    Public Function GetInIKeyValue(ByVal SectionName As String, _
                                   ByVal KeyName As String, _
                                   ByVal FileName As String) As String
        Dim KeyValue$
        Dim strTmp As String
        
        KeyValue$ = String$(512, " ")
        GetPrivateProfileString SectionName, KeyName, "", KeyValue$, 512, FileName
        strTmp = Trim(KeyValue$)
        GetInIKeyValue = Left(strTmp, Len(strTmp) - 1)
    End Function'********************************************************************************
    '** 函数功能:从ini配置文件中写入指定段名、关键字名及值
    '** 调用语法: SetInIKeyValue(SectionName as string,KeyName As String,KeyValue as string ,FileName As String)
    '** 参数说明:
    '**         SectionName :段名
    '**         KeyName     :关键字名
    '**         KeyValue    :关键字值
    '**         FileName    :ini文件名包括路径
    '** 返 回 值:
    '** 处理说明:
    '**         调用API函数WritePrivateProfileString
    '******************************************************************************
    Public Sub SetInIKeyValue(ByVal SectionName As String, _
                               ByVal KeyName As String, _
                               ByVal KeyValue As String, _
                               ByVal FileName As String)
        Dim lng As Long
        
        lng = WritePrivateProfileString(SectionName, KeyName, KeyValue, FileName)
    End Sub'********************************************************************************
    '** 函数功能:读取系统目录路径
    '** 调用语法: GetSysDir()
    '** 参数说明:
    '** 返 回 值:
    '**         String      :系统目录
    '** 处理说明:
    '**         调用API函数GetSystemDirectory
    '******************************************************************************
    Public Function GetSysDir() As String
        Dim sysDir$
        Dim strTmp As String
        
        sysDir = String$(128, " ")
        GetSystemDirectory sysDir$, 127
        strTmp = Trim(sysDir$)
        GetSysDir = Left(strTmp, Len(strTmp) - 1)
    End Function
      

  4.   

    在程序中怎么调用这个.INI文件呢?
      

  5.   

    用 GetSysDir & "\*.ini 来取得对应的INI文件
      

  6.   

    我的建议是用一个文本文件保存连接字符,然后每次程序运行时,就直接读果个文本文件的内容.
    我有一个A.TXT
    在公用模块声明
    PUBLIC CONSTR AS STRING
    SUB MAIN()
    OPEN 路径+"A.TXT" for input as #1
    line input #1,constr
    close #1
    END SUB
    然后系其它窗体用