我用VB做了ODBC的连接,在程序中如何实现自动配置,ODBC的驱动为SQL SERVER
解决方案 »
- 获取其他窗口是否存在
- 上哪里去找jet351.msm和jet40.msm? 现在用installshield X做安装程序无法继续啦!
- 求一稍为复杂的SQL语句的查询,晕了N久了,望高手指点呀在线等...
- 关于 利用Adobe Illustrator 把.AI文件 轉成 .pdf(200分)
- 怎样用VB连接sybase sql anywhere5.0数据库
- 高分求教:关于function pointer,在线等待,急
- VB MSChart控件遇到的问题
- msChart一个属性怎么写
- 再次100分问!关于MSFLEXGRID的问题
- 急!怎么实现datareport的分组打印?要多少分给多少分!!
- 快救我吧!分层显示问题
- 急问:当我的picturebox里的控件大于picturebox的大小时?
不过我不提倡要配置ODBC
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