试试这个: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_REMOVE_SYS_DSN = 6
Private Const vbAPINull As Long = 0&
Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As String) As LongPublic Sub CreateDSN(sDSN As String)
  On Error Resume Next
  Dim nRet As Long
  Dim sDriver As String
  Dim sAttributes As String
  sDriver = "SQl server"
  sAttributes = "DSN=" & sDSN & Chr$(0)
  sAttributes = sAttributes & "Server=(local)" & Chr$(0)
  sAttributes = sAttributes & "Database=webstation" & Chr$(0)
  nRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_SYS_DSN, sDriver, sAttributes)
End SubPublic Sub DeleteDSN(sDSN As String)
  On Error Resume Next
  Dim nRet As Long
  Dim sDriver As String
  Dim sAttributes As String
  sDriver = "SQl server"
  sAttributes = sAttributes & "DSN=" & sDSN & Chr$(0)
  nRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_SYS_DSN, sDriver, sAttributes)
End SubPrivate Sub Command1_Click()
   CreateDSN "mydsn"
End Sub
Private Sub Command2_Click()
   DeleteDSN "mydsn"
End Sub

解决方案 »

  1.   

    Dim fsn As New Scripting.FileSystemObject
    Dim myDsn As Scripting.TextStream
    Private Sub Command1_Click()
    CommonDialog1.DialogTitle = "Select Your Database Name"
    CommonDialog1.Filter = "mdb Files(*.mdb)|*.mdb"
    CommonDialog1.ShowOpen
    If CommonDialog1.FileName = Empty Then
    MsgBox "Please Select Your Database Name" & (Chr(13) & Chr(10)) & "And Also Your DSN Not Created", vbDefaultButton1 + vbInformation, "Viju"
    Else
    Set myDsn = fsn.CreateTextFile("c:\viju.dsn")
    myDsn.WriteLine "[ODBC]"
    myDsn.WriteLine "DRIVER=Microsoft Access Driver (*.mdb)"
    myDsn.WriteLine "UID = admin"
    myDsn.WriteLine "UserCommitSync = Yes"
    myDsn.WriteLine "Threads = 3"
    myDsn.WriteLine "SafeTransactions = 0"
    myDsn.WriteLine "PageTimeout = 5"
    myDsn.WriteLine "MaxScanRows = 8"
    myDsn.WriteLine "MaxBufferSize = 512"
    myDsn.WriteLine "ImplicitCommitSync = Yes"
    myDsn.WriteLine "FIL=MS Access"
    myDsn.WriteLine "DriverId = 25"
    myDsn.WriteLine "DefaultDir=C:\"
    myDsn.WriteLine "DBQ=" & CommonDialog1.FileName
    MsgBox "Your DSN is Created Now"
    End If
    End Sub