建立ODBC,利用ODBC api添加dns方法.
'Constant Declaration
Private Const ODBC_ADD_DSN = 1 ' Add data source
Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
Private Const vbAPINull As Long = 0& ' NULL Pointer
Private Declare Function SQLAllocEnv Lib "odbc32.dll" (phenv&;) As Integer
'Function Declare
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
Option ExplicitPrivate Sub Command1_Click()
Dim intRet As Long
Dim strDriver As String
Dim strAttributes As String
strDriver = "Microsoft Access Driver (*.mdb)"
'Set the attributes delimited by null.
'See driver documentation for a complete list of attributes.
strAttributes = "DSN=DSN_TEMP" & Chr$(0)
'To show dialog, use Form1.Hwnd instead of vbAPINull.
intRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN,
strDriver, strAttributes)
If intRet Then
MsgBox "DSN Deleted"
Else
MsgBox "Delete Failed"
End IfEnd SubPrivate Sub Command2_Click()
Dim intRet As Long
Dim strDriver As String
Dim strAttributes As String
Dim rc As Integer 'ODBC 函 数 的 返 回 码
Dim henv As Long 'ODBC 环 境 句 柄
rc = SQLAllocEnv(henv) ' 获 取ODBC 环 境 句 柄
strDriver = "Microsoft Access Driver (*.mdb)"
strAttributes = "SERVER=SomeServer" & Chr$(0)
strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)
strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr$(0)
strAttributes = strAttributes & "DBQ=d:\pdzl.mdb" & Chr$(0)
strAttributes = strAttributes & "UID=admin" & Chr$(0)
strAttributes = strAttributes & "PWD=" & Chr$(0)
'To show dialog, use Form1.Hwnd instead of vbAPINull.
intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, _
strDriver, strAttributes)
If intRet Then
MsgBox "DSN Created"
Else
MsgBox "Create Failed"
End If
End Sub
-------------------------------------------------------------------
'Constant Declaration
Private Const ODBC_ADD_DSN = 1 ' Add data source
Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
Private Const vbAPINull As Long = 0& ' NULL Pointer
Private Declare Function SQLAllocEnv Lib "odbc32.dll" (phenv&;) As Integer
'Function Declare
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
Option ExplicitPrivate Sub Command1_Click()
Dim intRet As Long
Dim strDriver As String
Dim strAttributes As String
strDriver = "Microsoft Access Driver (*.mdb)"
'Set the attributes delimited by null.
'See driver documentation for a complete list of attributes.
strAttributes = "DSN=DSN_TEMP" & Chr$(0)
'To show dialog, use Form1.Hwnd instead of vbAPINull.
intRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN,
strDriver, strAttributes)
If intRet Then
MsgBox "DSN Deleted"
Else
MsgBox "Delete Failed"
End IfEnd SubPrivate Sub Command2_Click()
Dim intRet As Long
Dim strDriver As String
Dim strAttributes As String
Dim rc As Integer 'ODBC 函 数 的 返 回 码
Dim henv As Long 'ODBC 环 境 句 柄
rc = SQLAllocEnv(henv) ' 获 取ODBC 环 境 句 柄
strDriver = "Microsoft Access Driver (*.mdb)"
strAttributes = "SERVER=SomeServer" & Chr$(0)
strAttributes = strAttributes & "DESCRIPTION=Temp DSN" & Chr$(0)
strAttributes = strAttributes & "DSN=DSN_TEMP" & Chr$(0)
strAttributes = strAttributes & "DBQ=d:\pdzl.mdb" & Chr$(0)
strAttributes = strAttributes & "UID=admin" & Chr$(0)
strAttributes = strAttributes & "PWD=" & Chr$(0)
'To show dialog, use Form1.Hwnd instead of vbAPINull.
intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, _
strDriver, strAttributes)
If intRet Then
MsgBox "DSN Created"
Else
MsgBox "Create Failed"
End If
End Sub
-------------------------------------------------------------------
先声明Connection,Command和Recordset对象,再用Connection或Command对象的Execute方法执行SQL语句(CREATE DATABASE创建数据库)。
dim adoConnection As New ADODB.Connection
dim strCon as string
strCon = "Provider=SQLOLEDB.1;User ID=sa;Database=sb;Server=Server;PWD=;"
With adoConnection
.ConnectionString = strCon
.Open
.CursorLocation = adUseClient
If .State = adStateOpen Then
'MsgBox adoConnection
Else
MsgBox "连接数据失败"
End If
End Withdim strQ as string
dim rs as new adodb.recordset
strQ="Sel......"
rs.open strQ,adoConnection,adOpenStatic, adLockReadOnly