如何在程序运行时,让它自动获得计算机名,来连结ADO得connection字符串?数据库为Sql server??

解决方案 »

  1.   

    说清楚是本机还是远程的。
    这是本机的:
    Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Declare Function SetComputerName Lib "kernel32" Alias "SetComputerNameA" (ByVal lpComputerName As String) As Long
    Public Function GetCName(CName As String) As Boolean
    Dim sName As String
    Dim lNLen As Long
    Dim lRes As Long
    Dim RV As Boolean
    lNLen = 256
    sName = Space(lNLen)
    lRes = GetComputerName(sName, lNLen)
    If lRes <> 0 Then
        CName = Left(sName, lNLen)
        RV = True
    Else
        RV = False
    End If
    GetCName = RV
    End Function
      

  2.   

    HKEY_CURRENT_USER\Software\Microsoft\Protected Storage System Provider\
    获得计算机名,实际上就是从注册表里读。
      

  3.   

    API:
     BOOL GetComputerName(
      LPTSTR lpBuffer,  // computer name
      LPDWORD lpnSize   // size of name buffer
    );
      

  4.   

    以下来自msdn 希望能够对你有帮助。
    The GetComputerName function retrieves the NetBIOS name of the local computer. This name is established at system startup, when the system reads it from the registry. If the local computer is a node in a cluster, GetComputerName returns the name of the node. Windows 2000 or later: GetComputerName retrieves only the NetBIOS name of the local computer. To retrieve the DNS host name, DNS domain name, or the fully qualified DNS name, call the GetComputerNameEx function.The GetComputerNameEx function retrieves a NetBIOS or DNS name associated with the local computer. The names are established at system startup, when the system reads them from the registry. GetComputerNameEx can retrieve the NetBIOS name retrieved by the GetComputerName function. In addition, GetComputerNameEx can retrieve fully qualified, host, and domain DNS names.If the local computer is a node in a cluster, GetComputerNameEx can return either the name of the cluster or the name of the local computer. BOOL GetComputerNameEx(
      COMPUTER_NAME_FORMAT NameType,  // name type
      LPTSTR lpBuffer,                // name buffer
      LPDWORD lpnSize                 // size of name buffer
    );
    Parameters
    NameType 
    [in] A value from the COMPUTER_NAME_FORMAT enumeration type that specifies the type of name to retrieve. 
    This parameter can be one of the following values from the COMPUTER_NAME_FORMAT enumeration type that specifies the type of name to retrieve. Value Description 
    ComputerNameNetBIOS The NetBIOS name of the local computer. If the local computer is a node in a cluster, lpBuffer receives the NetBIOS name of the cluster.  
    ComputerNameDnsHostname The DNS host name of the local computer. If the local computer is a node in a cluster, lpBuffer receives the DNS host name of the cluster.  
    ComputerNameDnsDomain The name of the DNS domain assigned to the local computer. If the local computer is a node in a cluster, lpBuffer receives the DNS domain name of the cluster.  
    ComputerNameDnsFullyQualified The fully qualified DNS name that uniquely identifies the local computer. This name is a combination of the DNS host name and the DNS domain name, using the form HostName.DomainName. If the local computer is a node in a cluster, lpBuffer receives the fully qualified DNS name of the cluster.  
    ComputerNamePhysicalNetBIOS The NetBIOS name of the local computer. If the local computer is a node in a cluster, lpBuffer receives the NetBIOS name of the local computer, not the name of the cluster.  
    ComputerNamePhysicalDnsHostname The DNS host name of the local computer. If the local computer is a node in a cluster, lpBuffer receives the DNS host name of the local computer, not the name of the cluster.  
    ComputerNamePhysicalDnsDomain The name of the DNS domain assigned to the local computer. If the local computer is a node in a cluster, lpBuffer receives the DNS domain name of the local computer, not the name of the cluster.  
    ComputerNamePhysicalDnsFullyQualified The fully qualified DNS name that uniquely identifies the computer. If the local computer is a node in a cluster, lpBuffer receives the fully qualified DNS name of the local computer, not the name of the cluster. 
    The fully qualified DNS name is a combination of the DNS host name and the DNS domain name, using the form HostName.DomainName. 
     lpBuffer 
    [out] Pointer to a buffer that receives a null-terminated string containing the computer name. 
    The length of the name may be greater than MAX_COMPUTERNAME_LENGTH characters because DNS allows longer names. lpnSize 
    [in/out] On input, specifies the size, in TCHARs, of the buffer. On output, receives the number of TCHARs copied to the destination buffer, including the terminating null character. 
    If the buffer is too small, the function fails and GetLastError returns ERROR_MORE_DATA. Return Value
    If the function succeeds, the return value is a nonzero value. If the function fails, the return value is zero. To get extended error information, call GetLastError. 
      

  5.   

    var
    u: array[0..127] of Char;
    c: array[0..127] of Char;
    user: string;
    computer: string;
    sz: dword;
    begin
    sz := SizeOf(u);
    GetUserName(u, sz);sz := SizeOf(c);
    GetComputerName(c, sz);user := u;
    computer := c;
    end;