我用VB做了ODBC的连接,在程序中如何实现自动配置,ODBC的驱动为SQL SERVER

解决方案 »

  1.   

    有人会回答的,
    不过我不提倡要配置ODBC
      

  2.   

    使用ado连接,连接信息可以放在ini或者注册表中
      

  3.   

    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 Long
    Private 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
       DSNCreate = SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, tAttributes.Driver, sAttributes)
    Else
       DSNCreate = SQLConfigDataSource(0&, ODBC_ADD_DSN, tAttributes.Driver, sAttributes)
    End If
    End FunctionPrivate Function DSNDelete(sDSN As String, sDriver As String, Optional bSystemDSN As Boolean = False) As Boolean
    Dim lRet As Long
    Dim sAttributes As String
    On Error Resume Next
    sAttributes = "DSN=" & sDSN & vbNullChar
    If bSystemDSN Then
       DSNDelete = SQLConfigDataSource(0&, ODBC_REMOVE_DSN, sDriver, sAttributes)
    Else
       DSNDelete = SQLConfigDataSource(0&, ODBC_REMOVE_SYS_DSN, sDriver, sAttributes)
    End If
    End FunctionSub Test()
    Dim tDSNDetails As tDSNAttrib
    '---Add an Access DSN
    With tDSNDetails
    .Database = "C:\vbusers.mdb"
    .Driver = "Microsoft Access Driver (*.mdb)"
    .Password = ""
    .UserID = "Admin"
    .DSN = "TestDSN"
    .Description = "A Test Database"
    .Type = FileBased
    End With
    If DSNCreate(tDSNDetails) Then
    MsgBox "Created user DSN"
    'Delete the new DSN
    If DSNDelete(tDSNDetails.DSN, tDSNDetails.Driver) Then
    MsgBox "Deleted New DSN"
    Else
    MsgBox "Failed to Delete New DSN"
    End If
    Else
    MsgBox "Failed to Create DSN"
    End If
    '---Add an SQL Server DSN
    With tDSNDetails
    .Database = "Pubs"
    .Driver = "SQL Server"
    .Server = "MyServer"
    .TrustedConnection = True    'Use NT authentication
    .Password = ""
    .UserID = ""
    .DSN = "TestDSN2"
    .Description = "A Test Database2"
    .Type = ServerBased
    .SystemDSN = True           'Create a System DSN
    End With
    If DSNCreate(tDSNDetails) Then
    MsgBox "Created system DSN"
    'Delete the new DSN
    If DSNDelete(tDSNDetails.DSN, tDSNDetails.Driver) Then
    MsgBox "Deleted New DSN"
    Else
    MsgBox "Failed to Delete New DSN"
    End If
    Else
    MsgBox "Failed to Create DSN"
    End If
    End SubPrivate Sub Form_Load()
      Call Test
    End Sub