试试这个: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
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
解决方案 »
- 如何查找隐藏进程
- 请问vb中如何建立文件,在多级目录下?
- 当与TDBGRID绑定时,为什么在ADODC中记录不为空时直接赋值出错?
- 組件調用异常狀況﹐在線等待... ...
- 关于按钮的一个小问题,请教大虾们
- 如何用vb实现将数据写入到excel和word中。请附源代码!
- 请问如何禁止FLASH的弹出菜单。
- 菜鸟求助,散50分:老师要我们编一个局域网聊天程序,但是我不会,请给我提示!!!
- VB中有没有求两个数的最大值的函数?
- 变量的作用域如何设置?
- Access支不支持外连接(jion)?
- 在VB中的窗体的borderstyle=sizeable toolbar这个属性有什么用,设为这属性后,在MDI中运行这个窗体,它并没有像工具栏一样会停靠在主窗
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