strDriver = "MySQL ODBC 3.51 Driver" 'set the attributes delimited by null.
'See driver documentation for a complete
'list of supported attributes.
strAttributes = "SERVER=" & pstrServer & Chr(0)
strAttributes = strAttributes & "DESCRIPTION=" & pstrDescription & Chr(0)
strAttributes = strAttributes & "DSN=" & pstrDSN & Chr(0)
strAttributes = strAttributes & "DATABASE=" & pstrDataBase & Chr(0)
strAttributes = strAttributes & "UID=" & userid & Chr$(0)
strAttributes = strAttributes & "PWD=" & password & Chr$(0)
strAttributes = strAttributes & "Port=3306" & Chr$(0)
strAttributes = strAttributes & "Character set=gbk"
'Calls API to create DSN
CreateDSN = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)如上,除了红色部分其它都正常,请问Characterset这个参数是怎么写的?为什么我这样传不进去?
'See driver documentation for a complete
'list of supported attributes.
strAttributes = "SERVER=" & pstrServer & Chr(0)
strAttributes = strAttributes & "DESCRIPTION=" & pstrDescription & Chr(0)
strAttributes = strAttributes & "DSN=" & pstrDSN & Chr(0)
strAttributes = strAttributes & "DATABASE=" & pstrDataBase & Chr(0)
strAttributes = strAttributes & "UID=" & userid & Chr$(0)
strAttributes = strAttributes & "PWD=" & password & Chr$(0)
strAttributes = strAttributes & "Port=3306" & Chr$(0)
strAttributes = strAttributes & "Character set=gbk"
'Calls API to create DSN
CreateDSN = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, strDriver, strAttributes)如上,除了红色部分其它都正常,请问Characterset这个参数是怎么写的?为什么我这样传不进去?
Public Const ODBC_CONFIG_DSN = 2 '设置(编辑)数据源
Public Const ODBC_REMOVE_DSN = 3 '移除数据源
Public Const vbAPINull As Long = 0& '空指针Public Declare Function SQLConfigDataSource Lib "odbccp32.dll" ( _
ByVal hwndParent As Long, _
ByVal fRequestas As Integer, _
ByVal lpszDriver As String, _
ByVal lpszAttributes As String) As Long
窗体模块代码:Option Explicit
Dim strConnect As String
Dim strSql As String
Dim cnnP As ADODB.Connection
Dim adoP As ADODB.Recordset
Dim lngP As Long
Dim strUserName As String
'Windows API函数,取得操作系统用户名
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long
'Windows API函数,消息提示
Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" _
(ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
'查询数据库
Private Sub btnSQL_Click()
On Error GoTo errClick
Set cnnP = New ADODB.Connection
' strConnect = "Data Source=DreamTeam;User=sa;Password=sa;InitialCatalog=Team"
' cnnP.ConnectionString = "DSN=DreamTeam"
cnnP.Provider = "SQLOLEDB"
' cnnP.ConnectionString = strConnect
cnnP.Open
strSql = "Select * From TB_Team"
Set adoP = New ADODB.Recordset
Set adoP.ActiveConnection = cnnP
adoP.CursorLocation = adUseServer
adoP.CursorType = adOpenKeyset
adoP.LockType = adLockOptimistic
adoP.Open strSql, cnnP, adOpenDynamic, adLockBatchOptimistic
adoP.MoveFirst
Do
msgTable.AddItem adoP!FD_Name & Chr(9) & adoP!FD_Age & Chr(9) & adoP!FD_Number
adoP.MoveNext
Loop Until adoP.EOF
Set adoP = Nothing
Set cnnP = Nothing
Exit Sub
errClick:
MessageBox hwnd, Err.Description, "系统提示", vbOKOnly + vbExclamation
End Sub
'装载窗体
Private Sub Form_Load()
Dim lngP As Long
Dim sDriver As String
Dim sAttributes As String
On Error GoTo errLoad
With msgTable
.Cols = 3
.Rows = 1
.FormatString = ">姓 名|^年 纪|^号 码"
.ColWidth(0) = 1000
.ColWidth(1) = 2000
.ColWidth(2) = 4000
End With
Rem 动态注册ODBC数据源
Rem Access数据源
' lngP = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, _
' "Microsoft Access Driver (*.mdb)" & Chr$(0), _
' "DSN=DreamTeamAccess;DBQ=" & App.Path & "\DBList.mdb;DEFAULTDIR=" & App.Path & Chr$(0))
Rem SQL Server数据源
sDriver = "SQL Server"
sAttributes = "DSN=" & "DreamTeamSql" & Chr$(0)
sAttributes = sAttributes & "Server=" & "ZhangWei" & Chr$(0)
sAttributes = sAttributes & "Database=DB_Team"
lngP = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, sDriver, sAttributes)
If lngP = 0 Then
MessageBox hwnd, "创建ODBC数据源失败", "系统提示", vbOKOnly + vbExclamation
Else
MessageBox hwnd, "创建ODBC数据源成功", "系统提示", vbOKOnly + vbExclamation
End If
Exit Sub
errLoad:
MessageBox hwnd, Err.Description, "系统提示", vbOKOnly + vbExclamation
End Sub
'卸载窗体,同时删除ODBC数据源
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim lngP As Long
Rem 动态注销ODBC数据源
' lngP = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, _
' "Microsoft Access Driver (*.mdb)" & Chr$(0), _
' "DSN=DreamTeamAccess" & Chr$(0))
lngP = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, _
"SQL Server" & Chr$(0), _
"DSN=DreamTeamAccess" & Chr$(0))
If lngP = 0 Then
MessageBox hwnd, "删除ODBC数据源失败", "系统提示", vbOKOnly + vbExclamation
Else
MessageBox hwnd, "删除ODBC数据源成功", "系统提示", vbOKOnly + vbExclamation
End If
Exit Sub
End Sub
CHARSET=gbk
结帖