惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V2EX - 技术
V2EX - 技术
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
GbyAI
GbyAI
The Cloudflare Blog
小众软件
小众软件
MyScale Blog
MyScale Blog
IT之家
IT之家
H
Help Net Security
宝玉的分享
宝玉的分享
V
Visual Studio Blog
Hugging Face - Blog
Hugging Face - Blog
F
Fortinet All Blogs
博客园_首页
S
SegmentFault 最新的问题
MongoDB | Blog
MongoDB | Blog
The Hacker News
The Hacker News
有赞技术团队
有赞技术团队
Microsoft Security Blog
Microsoft Security Blog
Know Your Adversary
Know Your Adversary
Project Zero
Project Zero
P
Palo Alto Networks Blog
博客园 - 聂微东
罗磊的独立博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Engineering at Meta
Engineering at Meta
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
U
Unit 42
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
Cisco Talos Blog
Cisco Talos Blog
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cyberwarzone
Cyberwarzone
G
Google Developers Blog
C
Cybersecurity and Infrastructure Security Agency CISA
大猫的无限游戏
大猫的无限游戏
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
The GitHub Blog
The GitHub Blog
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 热门话题
T
Tailwind CSS Blog
雷峰网
雷峰网
C
Cisco Blogs

博客园 - czh

基于MDB_ICP协议的纸币识别器与自动售货机通讯的研究 Mobile Network auto-Connection How could you login to ms sql server if you lost system admin password to setup windows auto-login Compile DLLs referenced into exe/dll 族谱软件系统的使用介绍 javascript在vs2003中调试随笔和javascript使用小总结[转载] 世界互联网正处于剧变前夜! 系统分析师 考试 DVD playable asp.net 页面事件:顺序与回传 创业还是读研? C#判断文件名是否合法 Window消息大全 关于STRONG NAME 好东西 一名25岁的董事长给大学生的18条忠告(不想虚度年华的进来) ZT SQL*PLUS命令的使用大全 ASP连接11种数据库语法总结
如何做一个增加系统DSN的过程
czh · 2006-04-05 · via 博客园 - czh

Public   Enum   e_DSNtype
        eUserDSN   =   0         '用户数据源
        eSysDSN                   '系统数据源
End   Enum

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   Const   SQL_SUCCESS   As   Long   =   0
Private   Const   SQL_ERROR   As   Long   =   -1
Private   Const   SQL_FETCH_NEXT   As   Long   =   1

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   Declare   Function   SQLDataSources   Lib   "ODBC32.DLL"   (ByVal   henv   As   Long,   ByVal   fDirection   As   Integer,   ByVal   szDSN   As   String,   ByVal   cbDSNMax   As   Integer,   pcbDSN   As   Integer,   ByVal   szDescription   As   String,   ByVal   cbDescriptionMax   As   Integer,   pcbDescription   As   Integer)   As   Integer
Private   Declare   Function   SQLAllocEnv   Lib   "ODBC32.DLL"   (env   As   Long)   As   Integer

'创建数据源       成功返回TRUE,失败返回FALSE
Public   Function   fun_CreateDSN(ByVal   DSNname   As   String,   ByVal   ODBCdriver   As   String,   ByVal   DSNtype   As   e_DSNtype,   _
                                                            ByVal   SVRname   As   String,   ByVal   DBname   As   String,   ByVal   User   As   String,   _
                                                            ByVal   pwd   As   String,   ByVal   DSNdesc   As   String)   As   Boolean
'         DSNname:数据源名
'         ODBCdriver:数据源驱动
'         DSNtype:数据源类型(系统、用户)
'         SVRname:服务器名称
'         DBname:数据库名
'         User:用户名
'         PWD:密码
'         DSNdesc:数据源描述
        On   Error   Resume   Next
        Dim   nRet   As   Long
        Dim   sAttributes   As   String
        If   DSNname   <>   ""   Then   sAttributes   =   "DSN="   &   DSNname   &   Chr$(0)
        If   DSNdesc   <>   ""   Then   sAttributes   =   sAttributes   &   "DESCRIPTION="   &   DSNdesc   &   Chr$(0)
        If   SVRname   <>   ""   Then   sAttributes   =   sAttributes   &   "SERVER="   &   SVRname   &   Chr$(0)
        If   User   <>   ""   Then   sAttributes   =   sAttributes   &   "UID="   &   User   &   Chr$(0)
        If   pwd   <>   ""   Then   sAttributes   =   sAttributes   &   "PWD="   &   pwd   &   Chr$(0)
        If   InStr(1,   LCase$(ODBCdriver),   "access")   >   0   Then
                If   DBname   <>   ""   Then   sAttributes   =   sAttributes   &   "DBQ="   &   DBname   &   Chr$(0)
        ElseIf   InStr(1,   LCase$(ODBCdriver),   "sql   server")   >   0   Then
                sAttributes   =   "DSN="   &   DSNname   &   Chr$(0)   &   "Server="   &   SVRname   &   Chr$(0)   &   _
                                        "UseProcForPrepare=Yes"   &   Chr$(0)
        Else
                If   DBname   <>   ""   Then   sAttributes   =   sAttributes   &   "DATABASE="   &   DBname   &   Chr$(0)
        End   If
        If   DSNtype   =   eSysDSN   Then
                nRet   =   SQLConfigDataSource(vbAPINull,   ODBC_ADD_SYS_DSN,   ODBCdriver,   sAttributes)
        ElseIf   DSNtype   =   eUserDSN   Then
                nRet   =   SQLConfigDataSource(vbAPINull,   ODBC_ADD_DSN,   ODBCdriver,   sAttributes)
        Else
                fun_CreateDSN   =   False
                Exit   Function
        End   If
        If   nRet   =   0   Then
                fun_CreateDSN   =   False
        Else
                fun_CreateDSN   =   True
        End   If
End   Function

'删除数据源,成功返回TRUE,失败返回FALSE
Public   Function   fun_DeleteDSN(ByVal   DSNname   As   String,   ByVal   ODBCdriver   As   String,   ByVal   DSNtype   As   e_DSNtype)   As   Boolean
'         DSNname:数据源名
'         ODBCdriver:数据源驱动
'         DSNtype:数据源类型(系统、用户)
        On   Error   Resume   Next
        Dim   nRet   As   Long
        Dim   sAttributes   As   String
        sAttributes   =   sAttributes   &   "DSN="   &   DSNname   &   Chr$(0)
        If   DSNtype   =   eSysDSN   Then
                nRet   =   SQLConfigDataSource(vbAPINull,   ODBC_REMOVE_SYS_DSN,   ODBCdriver,   sAttributes)
        ElseIf   DSNtype   =   eUserDSN   Then
                nRet   =   SQLConfigDataSource(vbAPINull,   ODBC_REMOVE_DSN,   ODBCdriver,   sAttributes)
        Else
                fun_DeleteDSN   =   False
                Exit   Function
        End   If
        If   nRet   =   0   Then
                fun_DeleteDSN   =   False
        Else
                fun_DeleteDSN   =   True
        End   If
End   Function

'搜索系统中所有的DSN(ODBC)数据源和对应的驱动         0表示失败,其余数值表示返回的驱动或数据源的数量(数组从1开始)
Public   Function   fun_GetDSNsAndDrivers(ByRef   strArr_DSNs()   As   String,   ByRef   strArr_DRVs()   As   String)   As   Long
        Dim   int_Ret   As   Integer
        Dim   sDSNItem   As   String   *   1024
        Dim   sDRVItem   As   String   *   1024
        Dim   int_I   As   Integer
        Dim   sDSN   As   String
        Dim   sDRV   As   String
        Dim   iDSNLen   As   Integer
        Dim   iDRVLen   As   Integer
        Dim   lHenv   As   Long           '对环境处理
       
        On   Error   GoTo   doError
       
        ReDim   strArr_DSNs(0)   As   String
        ReDim   strArr_DRVs(0)   As   String
        If   SQLAllocEnv(lHenv)   <>   SQL_ERROR   Then
                int_I   =   0
                sDSNItem   =   Space(1024)
                sDRVItem   =   Space(1024)
                int_Ret   =   SQLDataSources(lHenv,   SQL_FETCH_NEXT,   sDSNItem,   1024,   iDSNLen,   sDRVItem,   1024,   iDRVLen)
                Do   Until   int_Ret   <>   SQL_SUCCESS
                        sDSN   =   Left$(sDSNItem,   iDSNLen)
                        sDRV   =   Left$(sDRVItem,   iDRVLen)
                        int_I   =   int_I   +   1
                        If   int_I   =   1   Then
                                ReDim   strArr_DSNs(1   To   1)   As   String
                                ReDim   strArr_DRVs(1   To   1)   As   String
                        Else
                                ReDim   Preserve   strArr_DSNs(1   To   int_I)   As   String
                                ReDim   Preserve   strArr_DRVs(1   To   int_I)   As   String
                        End   If
                        strArr_DSNs(int_I)   =   sDSN
                        strArr_DRVs(int_I)   =   sDRV
                        sDSNItem   =   Space(1024)
                        sDRVItem   =   Space(1024)
                        int_Ret   =   SQLDataSources(lHenv,   SQL_FETCH_NEXT,   sDSNItem,   1024,   iDSNLen,   sDRVItem,   1024,   iDRVLen)
                Loop
                fun_GetDSNsAndDrivers   =   int_I
        Else
                fun_GetDSNsAndDrivers   =   0
        End   If
        Exit   Function
doError:
        fun_GetDSNsAndDrivers   =   0
End   Function