我是用的VB6,在写连接串的时候,需要得到当前数据库服务器的名字,以前都是写死的。如果程序一移动到另外一个机器,数据库服务器名字不一样,就会在连接串需要修改,请问VB6里怎么样写一段程序,可以自动获得本机的数据库服务器名字?

解决方案 »

  1.   

    把服务器名字放入INI文件中,以后你要移动到另外一个机器,只要把INI文件修改下就行了
      

  2.   

    能自动获得吗?INI文件怎么做?
      

  3.   

    http://www.shootsoft.net/home/show.aspx?id=17&cid=8 里面有读写INI的模块和使用方法昨天我也问了,不是太明白,你看你能看懂怎么做的不
      

  4.   

    下面是连接SQL SERVER数据库的,你参考下INI文件 
    'Option Explicit  
    '保存执行SQL语句的字符串  
    Public sqlstmt As String  
    '声明写入INI文件的API函数  
    Public 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  
    '定义服务器参数常量  
    Public server As String  
    Public user As String  
    Public password As String  '声明类模块变量  
    Public mydb As New clsdb  
    Public mybusinessman As New Clsbusinessman  
    Public mycustomer As New Clscustomer  
    Public mydeliver As New Clsdeliver  
    Public mybusiness As New Clsbusiness  
    '程序进入点  
    Sub main()  
    '从setup.ini中读取服务器信息  
        server = getkey(App.Path + "\setup.ini", "server")  
        user = getkey(App.Path + "\setup.ini", "user")  
        password = getkey(App.Path + "\setup.ini", "password")  '如果读取不成功,退出  
    If server = "" Then  
        MsgBox "setup.ini格式不正确,请重新设置"  
        End  
    End If  ''设置数据环境器参数  
    'DataEnvironmentclient.Client.ConnectionString = "driver={sql server};server=" + Trim(server) + ";uid=" + Trim(user) + ";pwd=" + Trim(password) + ";database=erp1"  '显示主窗体  
    Frmmanclient.Show  
    End Sub  Function fileexist(fname As String) As Boolean  
    '判断INI文件是否存在  
    On Local Error Resume Next  
        fileexist = (Dir(fname)   <> "")  
    End Function  
    Public Function getkey(tmp_file As String, tmp_key As String) As String  
    Dim file As Long  
    '分配文件句柄  
    file = FreeFile  
    '如果文件不存在则创建一个默认的SETUP.INI文件  
    If fileexist(tmp_file) = False Then  
        getkey = ""  
        Call WritePrivateProfileString("setup information", "server", "", App.Path + "\setup.ini")  
        Call WritePrivateProfileString("setup information", "username", "", App.Path + "\setup.ini")  
        Call WritePrivateProfileString("setup information", "password", "", App.Path + "\setup.ini")  
        Exit Function  
    End If  
    '读取数据项值  
    Open tmp_file For Input As file  
    Do While Not EOF(1)  
        Line Input #file, buffer  
        If Left(buffer, Len(tmp_key)) = tmp_key Then  
            pos = InStr(buffer, "=")  
            getkey = Trim(Mid(buffer, pos + 1))  
        End If  
    Loop  
    Close file  
    End Function  
      

  5.   

    顺便问下,服务器换了.ini文件读取服务器名,用户名...是自动读取还是要手动,昨天就没弄明白这点
      

  6.   

    如果仅仅是 可以自动获得本机的数据库服务器名字 的话
    那在写连接串的时候,把数据库服务器的名字 改为localhost 或127.0.0.1 即可。
    其它什么都不用变