VB编程中如何用当前windows2000登录用户名去连接访问SQL2000?连接中User ID和password该如何填写,请给出范列

解决方案 »

  1.   

    Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As LongPrivate conn As New ADODB.Connection
    Private rs As New ADODB.Recordset
    Dim loginname As String
    Private Sub Form_Load()
        
        loginname=user_name '先取得登錄名
        call OpenDB   '打開數據庫
    end sub
    Private Function user_name() As String
        Dim str As String
        Dim res As Long
        str = String(1024, 0)
        res = GetUserName(str, 1024)
        If res <> 0 Then
            user_name = Mid(str, 1, InStr(1, str, Chr(0)) - 1)
        Else
            user_name = ""
        End If
    End FunctionPrivate Sub OpenDB()
        With conn
            If .State = adStateOpen Then
                .Close
            End If
            .CursorLocation = adUseClient
            .CommandTimeout = 0
            .Mode = adModeReadWrite
            .Open "Provider=SQLOLEDB.1;Password=*****;Persist Security Info=True;" & _
                            "User ID='"& loginname &"';Initial Catalog=數據庫名;Data Source=aaaa"
        End With
    End Sub
      

  2.   

    dim cnn as new adodb.connection
    cnn.open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=數據庫名;Data Source=服務器名"