能否给出一个创建DSN的代码?
如何检测系统是否存在一个指定名称的DSN?
如果不存在则创建一个。
数据库是MS SQL 2000的,用户sa口令空。
谢谢!
问题点数:0、回复次数:8Top
1 楼czwwh(沙·月)回复于 2004-04-03 13:21:43 得分 0
上这里看看,其实在网上搜一下,能找到的:
http://www.3552808.com/gy/dl/Article_Show.asp?ArticleID=39Top
2 楼czwwh(沙·月)回复于 2004-04-03 13:28:51 得分 0
这里还有一篇
http://expert.csdn.net/Expert/topic/2734/2734075.xml?temp=1.466006E-02Top
3 楼cuizm(射天狼 http://www.j2soft.cn/)回复于 2004-04-03 13:51:26 得分 0
StrAttributes = "DSN=orient_oa " & Chr(0) & "Database=orientnbcws " & Chr(0)
StrAttributes = StrAttributes & "Description=MySQL ODBC 3.51 Driver DSN " & Chr(0)
StrAttributes = StrAttributes & "Option=3 " & Chr(0) & "Password= " & Chr(0) & "Port=3306 " & Chr(0)
StrAttributes = StrAttributes & "Server=server " & Chr(0) & "Stmt= " & Chr(0) & "User= "
'建立 ODBC 数据源
Call LoadDbSource2( "MySQL ODBC 3.51 Driver ", StrAttributes)
'连接数据库 通过ODBC
cn.ConnectionString = "DSN=orient_oa;UID=orientwarn;PWD=; "
'在模块中
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
'建立数据源
Public Function LoadDbSource2(StrDriver, StrAttributes As String) As Boolean
LoadDbSource2 = SQLConfigDataSource(0&, ODBC_ADD_SYS_DSN, StrDriver, StrAttributes)
End FunctionTop
4 楼kissoflife(明月高楼休独倚,酒入愁肠,化作相思泪!)回复于 2004-04-03 14:15:36 得分 0
声明:
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hkey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32" (ByVal hkey As Long) As Long
' Reg Key Security Options...
Const READ_CONTROL = &H20000
Const KEY_QUERY_VALUE = &H1
Const KEY_SET_VALUE = &H2
Const KEY_CREATE_SUB_KEY = &H4
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const KEY_CREATE_LINK = &H20
' Reg Key ROOT Types...
Public Const HKEY_LOCAL_MACHINE = &H80000002
Const ERROR_SUCCESS = 0
Const REG_SZ = 1 ' Unicode nul terminated string
Const REG_DWORD = 4 ' 32-bit number
Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
'访问注册表值
Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String, ByRef KeyVal As String) As Boolean
Dim i As Long ' Loop Counter
Dim rc As Long ' Return Code
Dim hkey As Long ' Handle To An Open Registry Key
Dim hDepth As Long '
Dim KeyValType As Long ' Data Type Of A Registry Key
Dim tmpVal As String ' Tempory Storage For A Registry Key Value
Dim KeyValSize As Long ' Size Of Registry Key Variable
'------------------------------------------------------------
' Open RegKey Under KeyRoot {HKEY_LOCAL_MACHINE...}
'------------------------------------------------------------
rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hkey) ' Open Registry Key
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' Handle Error...
tmpVal = String$(1024, 0) ' Allocate Variable Space
KeyValSize = 1024 ' Mark Variable Size
'------------------------------------------------------------
' Retrieve Registry Key Value...
'------------------------------------------------------------
rc = RegQueryValueEx(hkey, SubKeyRef, 0, _
KeyValType, tmpVal, KeyValSize) ' Get/Create Key Value
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' Handle Errors
If (Asc(Mid(tmpVal, KeyValSize, 1)) = 0) Then ' Win95 Adds Null Terminated String...
tmpVal = Left(tmpVal, KeyValSize - 1) ' Null Found, Extract From String
Else ' WinNT Does NOT Null Terminate String...
tmpVal = Left(tmpVal, KeyValSize) ' Null Not Found, Extract String Only
End If
'------------------------------------------------------------
' Determine Key Value Type For Conversion...
'------------------------------------------------------------
Select Case KeyValType ' Search Data Types...
Case REG_SZ ' String Registry Key Data Type
KeyVal = tmpVal ' Copy String Value
Case REG_DWORD ' Double Word Registry Key Data Type
For i = Len(tmpVal) To 1 Step -1 ' Convert Each Bit
KeyVal = KeyVal + Hex(Asc(Mid(tmpVal, i, 1))) ' Build Value Char. By Char.
Next
KeyVal = Format$("&h" + KeyVal) ' Convert Double Word To String
End Select
GetKeyValue = True ' Return Success
rc = RegCloseKey(hkey) ' Close Registry Key
Exit Function ' Exit
GetKeyError: ' Cleanup After An Error Has Occured...
KeyVal = "" ' Set Return Val To Empty String
GetKeyValue = False ' Return Failure
rc = RegCloseKey(hkey) ' Close Registry Key
End Function
'写注册表
Public Sub SaveString(hkey As Long, strPath As String, strValue As String, strData As String)
Dim ret
'Create a new key
RegCreateKey hkey, strPath, ret
'Save a string to the key
RegSetValueEx ret, strValue, 0, REG_SZ, ByVal strData, Len(strData)
'close the key
RegCloseKey ret
End SubTop
5 楼kissoflife(明月高楼休独倚,酒入愁肠,化作相思泪!)回复于 2004-04-03 14:18:13 得分 0
具体的附加代码:
'******************************************************************************
' 以下代码判断数据源是否存在
' 如果不存在,通过直接写注册表来建立ODBC
' 获取ODBC中的数据源名
'******************************************************************************
strDatabase = GetINI(gstrCurrPath & DSNINIFile, "Database", "DSN", "?")
If strDatabase = "?" Then
Screen.MousePointer = 0
MsgBox "数据源信息已被损坏,程序将用缺省值进行修复!", vbExclamation, "提示"
strDatabase = DatabaseName
Call WriteINI(gstrCurrPath & DSNINIFile, "Database", "DSN", strDatabase)
Screen.MousePointer = 11
End If
If Not GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYODBCLOC & strDatabase, "Database", strTemp) Then
'获取SQL Server驱动程序路径
If GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYDRIVERLOC, gREGDRIVERSUBKEY, strDriver) Then
'成功取得SQL Server驱动程序路径
'接下来获取本机用户名
'Create a buffer
CreateODBC:
strUserName = String(100, Chr$(0))
'Get the username
GetUserName strUserName, 100
'strip the rest of the buffer
strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
'把ODBC数据源信息写入注册表
SaveString HKEY_LOCAL_MACHINE, gREGKEYODBCLOC & strDatabase, "Driver", strDriver
SaveString HKEY_LOCAL_MACHINE, gREGKEYODBCLOC & strDatabase, "Server", strServer
SaveString HKEY_LOCAL_MACHINE, gREGKEYODBCLOC & strDatabase, "Database", strDatabase
SaveString HKEY_LOCAL_MACHINE, gREGKEYODBCLOC & strDatabase, "LastUser", strUserName
SaveString HKEY_LOCAL_MACHINE, gREGKEYODBCLOC & strDatabase, "Trusted_Connection", "Yes"
SaveString HKEY_LOCAL_MACHINE, gREGKEYODBCLOC & "ODBC Data Sources", strDatabase, "SQL Server"
Else
'如果第一种方式不成功,尝试第二种方式
If GetKeyValue(HKEY_LOCAL_MACHINE, gREGKEYDRIVERLOC_A, gREGDRIVERSUBKEY, strDriver) Then
'如果成功,跳到写注册表部分
GoTo CreateODBC
Else
'如果仍然失败,则提示后直接退出
MsgBox "创建ODBC数据源时出现异常:无法从注册表中获取SQLServer驱动程序路径。请用手工添加!", vbExclamation, "提示"
GoTo ExitLab
End If
End If
End If
'******************************************************************************
' 数据源ODBC处理完毕!
'******************************************************************************Top
6 楼rngd(Renegade)回复于 2004-04-03 17:00:58 得分 0
kissoflife(明月高楼休独倚,酒入愁肠,化作相思泪!)你代码里GetINI,WriteINI是什么函数?我不明白。
http://expert.csdn.net/Expert/topic/2734/2734075.xml?temp=1.466006E-02
这个我看了,我第一次用VB,看不太懂,哪位能解释一下?谢谢
是不是建立一个类模块:Class Name:clsManageDSN
另外在模块里(任意的)加入
Module:
'创建人:Ranma_true………………
这一段代码。
然后调用的时候怎么写?谁能给个例子,比我我创建一个名为test的系统DSN,本地数据服务器,数据库名为testdata,用户sa,口令为空。
下面这段又是什么意思?
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources]
"test"="SQL Server"
"testnt"="SQL Server"
[HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\test]
"Driver"="C:\\WINNT\\system32\\SQLSRV32.dll"
"Server"="zj"
"LastUser"="sa"
[HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\testnt]
"Driver"="C:\\WINNT\\system32\\SQLSRV32.dll"
"Server"="zj"
"LastUser"="Administrator"
"Trusted_Connection"="Yes"
然后调用的时候怎么写?谁能给个例子,比我我创建一个名为test的系统DSN,本地数据服务器,数据库名为testdata,用户sa,口令为空。Top
7 楼artoksxb(进取人生)回复于 2004-04-03 18:59:34 得分 0
学啊!Top
8 楼wumylove1234(毁于随)回复于 2004-04-03 19:17:51 得分 0
mark!Top




