Option Explicit
Public Enum eDBType
  FileBased
  ServerBased
End Enum
Private Type tDSNAttrib
  Type As eDBType                 'FileBased (eg Access) or ServerBased (eg. SQL Server)
  Server As String                'Database Server
  Description As String           'Database description
  DSN As String                   'The DSN Name
  Driver As String                'The Drive name
  Database As String              'Name or path of database
  UserID As String                'The UserID
  Password As String              'The User Password
  TrustedConnection As Boolean    'If True ignore the UserID and Password as will us NT
  SystemDSN As Boolean            'If True creates a system DSN, else creates a user DSN.
End Type
Private Const ODBC_ADD_DSN = 1
Private Const ODBC_CONFIG_DSN = 2
Private Const ODBC_REMOVE_DSN = 3
Private Const ODBC_ADD_SYS_DSN = 4
Private Const ODBC_CONFIG_SYS_DSN = 5
Private Const ODBC_REMOVE_SYS_DSN = 6
Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As String) As LongPrivate Function DSNCreate(tAttributes As tDSNAttrib) As Boolean
Dim lRet As Long
Dim sAttributes As String
On Error Resume Next
If tAttributes.Type = FileBased Then
   sAttributes = "DBQ=" & tAttributes.Database & vbNullChar
Else
  sAttributes = "Server=" & tAttributes.Server & vbNullChar
  sAttributes = sAttributes & "DATABASE=" & tAttributes.Database & vbNullChar
End IfsAttributes = sAttributes & "DSN=" & tAttributes.DSN & vbNullChar
If Len(tAttributes.Description) Then
   sAttributes = sAttributes & "DESCRIPTION=" & tAttributes.Description & vbNullChar
End IfIf tAttributes.TrustedConnection Then
   sAttributes = sAttributes & "Trusted_Connection=Yes" & vbNullChar
Else
   If Len(tAttributes.UserID) Then
      sAttributes = sAttributes & "UID=" & tAttributes.UserID & vbNullChar
   End If
   If Len(tAttributes.Password) Then
      sAttributes = sAttributes & "PWD=" & tAttributes.Password & vbNullChar
   End If
End If
If tAttributes.SystemDSN Then
Debug.Print sAttributes
   DSNCreate = SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, tAttributes.Driver, sAttributes)
Else
Debug.Print sAttributes
   DSNCreate = SQLConfigDataSource(0&, ODBC_ADD_DSN, tAttributes.Driver, sAttributes)
End If
End FunctionPrivate Sub Form_Load()
 Dim tDSNDetails As tDSNAttrib'---Add an SQL Server DSN
With tDSNDetails
.Database = "bbb"
.Driver = "SQL Server"
.Server = "CYF"
.TrustedConnection = True  'Use NT authentication
.Password = "jk"
.UserID = "jk"
.DSN = "guo"
.Description = "A Test Database2"
.Type = ServerBased
.SystemDSN = False           'Create a System DSN
End With
If DSNCreate(tDSNDetails) = True Then
MsgBox "Created user DSN"
End If
End Sub
不加用户名和口令一样可以添加ODBC
明天就要交了 帮下忙啦
没有用户名和口令客户端没办法登陆呀

解决方案 »

  1.   

    ODBC没有口令没有关系,调用ODBC连接数据库的时候加上就行了.
      

  2.   

    楼上老大 我是客户端的ODBC登陆服务器的数据库 没口令我登陆不进去呀
      

  3.   

    楼主的意思是不是用SQLConfigDataSource建立的DSN不能设置成“使用用户输入登陆ID和密码的SQL SERVER验证”对吗?
      

  4.   

    SQLConfigDataSource设置的用户名和口令又起什么作用呢
      

  5.   


    1、
    With tDSNDetails
    .Database = "bbb"
    .Driver = "SQL Server"
    .Server = "CYF"
    .TrustedConnection = False  '把这句改为FALSE
    .Password = "jk"
    .UserID = "jk"
    .DSN = "guo"
    .Description = "A Test Database2"
    .Type = ServerBased
    .SystemDSN = False           'Create a System DSN
    End With2、If tAttributes.TrustedConnection Then
      sAttributes = sAttributes & "Trusted_Connection=Yes" & vbNullChar
    Else
       If Len(tAttributes.UserID) Then
          'sAttributes = sAttributes & "UID=" & tAttributes.UserID & vbNullChar
       End If
       If Len(tAttributes.Password) Then
          'sAttributes = sAttributes & "PWD=" & tAttributes.Password & vbNullChar
       End If
    End If'不用设置UID和PWD3、这样你一定可以设置成功。4、在使用ODBC API连接时(API函数为:SQLConnect),这时设置密码!SQLConnect函数说明在:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odch21cpr_2.asp处
      

  6.   

    以下代码是使用ODBC API连接SQL SERVER的函数,如有不详细的地方,请发消息给我!'== 声明odbc32.dll中定义的ODBC API函数 ==
    Public Declare Function SQLAllocEnv Lib "odbc32.dll" (phenv&) As Integer
    Public Declare Function SQLAllocConnect Lib "odbc32.dll" (ByVal Henv&, phdbcd&) As Integer
    Public Declare Function SQLAllocStmt Lib "odbc32.dll" (ByVal Hdbc&, phstmt&) As Integer
    Public Declare Function SQLConnect Lib "odbc32.dll" (ByVal Hdbc&, ByVal szDSN$, _
    ByVal cbDSN%, ByVal szUID$, ByVal cbUID%, ByVal szPWD$, ByVal cbPWD%) As Integer
    Public Declare Function SQLColAttributes Lib "odbc32.dll" (ByVal Hstmt&, ByVal icol%, _
    ByVal fDescType%, ByVal rgbDesc As String, ByVal cbDescMax%, pcbDesc%, pfDesc&) As Integer
    Public Declare Function SQLDisconnect Lib "odbc32.dll" (ByVal Hdbc&) As Integer
    Public Declare Function SQLExecDirect Lib "odbc32.dll" (ByVal Hstmt&, ByVal szSqlStr$, _
    ByVal cbSqStr&) As Integer
    Public Declare Function SQLFetch Lib "odbc32.dll" (ByVal Hstmt&) As Integer
    Public Declare Function SQLFreeConnect Lib "odbc32.dll" (ByVal Hdbc&) As Integer
    Public Declare Function SQLFreeEnv Lib "odbc32.dll" (ByVal Henv&) As Integer
    Public Declare Function SQLFreeStmt Lib "odbc32.dll" (ByVal Hstmt&, ByVal fOption%) As Integer
    Public Declare Function SQLGetData Lib "odbc32.dll" (ByVal Hstmt&, ByVal icol%, ByVal fCType%, _
    ByVal rgbValue As String, ByVal cbValueMax&, pcbValue%) As Integer
    Public Declare Function SQLNumResultCols Lib "odbc32.dll" (ByVal Hstmt&, pccol%) As Integer
    Public Declare Function SQLGetDiagRec Lib "odbc32.dll" (ByVal HandleType%, ByVal Handle&, _
    ByVal RecNumber%, Sqlstate%, NativeErrorPtr%, MessageText As String, _
    ByVal Bufferlenchgth%, TextlenchgthPtr%)
    '== 标记数据库是否连接 ==
    Private IsConnect As Boolean
    '== 标记执行Connect()函数后,访问数据库的次数 ==
    Private Connect_Num As Integer
    Private Henv As Long     '环境句柄
    Private Hdbc As Long     '连接句柄
    Private Rc As Long
    Public Hstmt As Long'连接数据库
    Private Sub Connect()
      Dim TmpStat As Long  'SQLConnect()函数的返回值
      '如果连接标记为真,则返回。否则会出错
      If IsConnect = True Then
        Exit Sub
      End If
      '分配环境句柄,保存在变量Henv中
      If SQLAllocEnv(Henv) Then
        MsgBox "无法初始化ODBC环境!", , "ODBC API执行错误"
        End
      End If
      '根据环境句柄,分配连接句柄,保存在变量Hdbc中
      If SQLAllocConnect(Henv, Hdbc) Then
        MsgBox "无法连接ODBC!", , "ODBC API执行错误"
        End
      End If
      '根据连接句柄、数据源、用户名和密码连接指定的数据库
      TmpStat = SQLConnect(Hdbc, DSN, Len(DSN), DB_USER_NAME, _
                Lench(DB_USER_NAME), DB_PASSWORD, Len(DB_PASSWORD))
      '如果连接不成功则退出程序
      If TmpStat <> SQL_SUCCESS And _
         TmpStat <> SQL_SUCCESS_WITH_INFO Then
        MsgBox "无法获得连接句柄!", , "ODBC API执行错误"
        IsConnect = True
        Disconnect
        End
      End If
      '设置连接标记
      IsConnect = True
    End Sub'断开与数据库的连接
    Private Sub Disconnect()
      Dim Rc As Long
      '如果连接标记为假,标明已经断开连接,则直接返回
      If IsConnect = False Then
        Exit Sub
      End If
      '断开连接
      Rc = SQLDisconnect(Hdbc)
      '释放连接句柄
      Rc = SQLFreeConnect(Hdbc)
      '释放环境句柄
      Rc = SQLFreeEnv(Henv)
      IsConnect = False
    End Sub'使用Connect_Num控制数据库连接
    Public Sub DB_Connect()
      Connect_Num = Connect_Num + 1
      Connect
    End Sub
    Public Sub DB_Disconnect()
      If Connect_Num >= CONNECT_LOOP_MAX Then
        Connect_Num = 0
        Disconnect
      End If
    End Sub
    '强制关闭api方式访问的数据库,计数器复位
    Public Sub DBapi_Disconnect()
      Connect_Num = 0
      Disconnect
    End Sub'执行ODBC数据库操作语句
    Public Sub OdbcExt(ByVal TmpSQLstmt As String)
      '根据连接句柄,分配语句句柄
      If SQLAllocStmt(Hdbc, Hstmt) Then
        MsgBox "句柄分配失败", , "ODBC API执行错误"
        DBapi_Disconnect
        End
      End If
      '执行SQL语句,Lench是用户自定义函数,计算包含汉字的字符串长度
      If SQLExecDirect(Hstmt, TmpSQLstmt, Lench(TmpSQLstmt)) Then
        MsgBox "数据库访问语句执行失败", , "ODBC API执行错误"
        MsgBox TmpSQLstmt
        DBapi_Disconnect
        End
      End If
    End Sub'使用Data控件连接数据库,将执行指定的SQL语句
    Public Sub DBdataExt(TmpData As Data, ByVal TmpSource As String)
      '关闭已有的ODBC连接
      DBapi_Disconnect
      'Data控件连接数据库
      TmpData.Connect = "ODBC;DATABASE=" + DATABASE _
                      + ";UID=" + DB_USER_NAME + ";PWD=" _
                      + DB_PASSWORD + ";DSN=" + DB_NAME
      TmpData.RecordSource = TmpSource
      TmpData.Refresh
    End Sub