本站首页    管理页面    写新日志    退出


«August 2019»
123
45678910
11121314151617
18192021222324
25262728293031


公告
暂无公告...

我的分类(专题)

日志更新

最新评论

留言板

链接


Blog信息
blog名称:VFP及Sql Server拙笔
日志总数:46
评论数量:107
留言数量:0
访问次数:381819
建立时间:2005年5月12日




[VFP与SQL]VFP检测SQL Server的五个实例代码
文章收藏,  网上资源,  心得体会,  事件记录,  软件技术

老瓷 发表于 2011-12-19 15:31:49

** 需要指出的是,无论下面哪种方式的代码,都需要打开本机的网络共享,否则找不到SQL服务器** 例一 **************************************************** Use MyFll.FllSet Library To MyFlllcLocalName = Left(Sys(0),At("#",Sys(0))-2)lcLocalIP = DomainToIP(lcLocalName)? '本机名:'+ lcLocalName? '本机的 IP 为:'+ lcLocalIPIF lcLocalIP == '127.0.0.1'   ? '本机现在处于单机状态,未联接网络,或网络适配器停用!'ENDIF cStr=GetSqlServer()nCount=ALines(aServer,cStr,",")   &&切分生成数组?? "可连接的 SQL Server 服务器"+Transform(nCount)+"个,分别为:"For ii=1 to nCount    ? ii,aServer[ii] &&将名字显示出来EndFor Set Library To ** 例二 ***************************************************** Use SqlDMO.DLL, must install SQLServer or SqlDMOloSqlDMO=Createobject("SQLDMO.Application")loSvrs=loSqlDMO.ListAvailableSQLServers()lnSvrsCount=loSvrs.Count?If lnSvrsCount=0    ? "没有找到可用的 SQL Server 服务器。"Else    ? "可连接的 SQL Server 服务器"+Transform(lnSvrsCount)+"个,分别为:"    For ii=1 TO lnSvrsCount        ? ii,loSvrs.Item(ii)    EndforEndifRelease loSqlDMO ** 例三 ****************************************************** Use Windows API, Only VFP9#Define MAX_PREFERRED_LENGTH        -1#Define SV_TYPE_SQLSERVER           0x00000004 Declare Long NetServerEnum In netapi32 string ServerName, Long nlevel, ;        Long @ bufptr, Long prefmaxlen, long @ entriesread, ;        Long @ totalentries, Long servertype, string domain, Long resume_handleDeclare Long NetApiBufferFree In netapi32 Long bufptrDeclare Long lstrlenW In win32api String @ lpString If Version(5)<900   =MessageBox( '需要 vfp 9.0 运行环境!', 16, '错误!' )Else   ?   Store 0 To lnBuff, lnReadNums, lnTotNums   If 0 == NetServerEnum( Null, 100, @ lnBuff, MAX_PREFERRED_LENGTH, @ lnReadNums, ;                          @ lnTotNums, SV_TYPE_SQLSERVER, Null, 0) and lnReadNums > 0      ? "可连接的 SQL Server 服务器"+TRANSFORM(lnReadNums)+"个,分别为:"      For ii = 1 To lnReadNums          m.lnAddr = CToBin(Sys(2600, m.lnBuff + (m.ii-1)*8 + 4, 4), 'RS')          m.lcSrv = Sys(2600, m.lnAddr, 256)          ? ii,Left(Strconv(m.lcSrv, 6), lstrlenW(m.lcSrv))      Endfor   Else      ? '没有找到可用的 SQL Server 实例。'   Endif   If lnBuff > 0      NetApiBufferFree(lnBuff)   EndifEndifCLEAR DLLS ** 例四 ************************************************* 为了与其他例子类似处理,将本例中的Cursor或Table除去了,同时代码也作了相应修改。特注明!*-----------------------------------------------* 函数: 获取局域网内所有SqlServer* 设计: 红雨* 时间: 2005.04.01*-----------------------------------------------?? NetEnumSqlServer() Function NetEnumSqlServer()    Declare SHORT SQLBrowseConnect In odbc32 Integer ConnectionHandle, String InConnectionString, ;            Integer StringLength1, String @ OutConnectionString, Integer BufferLength, Integer @ StringLength2Ptr    Declare SHORT SQLAllocHandle In odbc32 Integer HandleType, Integer InputHandle, Integer @ OutputHandlePtr    Declare SHORT SQLFreeHandle In odbc32 Integer HandleType, Integer Handle    Declare SHORT SQLSetEnvAttr In odbc32 Integer EnvironmentHandle, Integer Attribute, ;            Integer ValuePtr, Integer StringLength     Local hEnv, hConn, cInString, cOutString, nLenOutString, nCnt, iCnt    m.nCnt = 0    m.hEnv = 0    m.hConn = 0    m.cInString = "DRIVER=SQL SERVER"    m.cOutString = Space(2048)    m.nLenOutString = 0    Local Array aServerList[1]     If SQLAllocHandle(1, 0, @hEnv) = 0        If SQLSetEnvAttr(m.hEnv, 200, 3, 0) = 0            If SQLAllocHandle(2, m.hEnv, @hConn) = 0                If SQLBrowseConnect(m.hConn, @cInString, Len(m.cInString), @cOutString, 2048, @nLenOutString) = 99                    m.nCnt = Alines(aServerList, Strextract(m.cOutString, '{', '}'), .T., ',')                    lcMsg = '可连接的 SQL Server 服务器'+TRANSFORM(m.nCnt)+'个,分别为:'                      For m.iCnt = 1 To m.nCnt                        lcMsg = lcMsg + Chr(13) + Chr(10) + Chr(9) + Transform(m.iCnt) + aServerList[iCnt]                    Endfor                Else                    lcMsg = '没有找到可用的 SQL Server 实例。'                Endif            Endif        Endif    Endif    Clear Dlls    Return lcMsgEndfunc************************************************* ** 例五 ************************** 此例与红雨先生的上例近似,也取自网络并作部分修改?? GetNetSqlServerApi() Function GetNetSqlServerApi()* Use ODBC API#Define SQL_HANDLE_ENV          1  #Define SQL_HANDLE_DBC          2  #Define SQL_NULL_HANDLE         0  #Define SQL_SUCCESS         0  #Define SQL_SUCCESS_WITH_INFO       1  #Define SQL_NEED_DATA           99  #Define SQL_ERROR           -1  #Define SQL_INVALID_HANDLE      -2  #Define SQL_ATTR_ODBC_VERSION       200  #Define SQL_OV_ODBC3            3  #Define SQL_NTS             -3  #Define ENUM_SERVERS_MAX_RET_LENGTH -1     lcDrvName = 'Driver={SQL Server}'  Declare short SQLAllocHandle In odbc32 short HandleType, Long InputHandle, Long @ OutputHandlePtrDeclare short SQLSetEnvAttr In odbc32 long EnvironmentHandle, Long nAttribute, Long ValuePtr, Long StringLengthDeclare short SQLBrowseConnect In odbc32 long ConnectionHandle, String InConnectionString, short StringLength1, ;        String @ OutConnectionString, short BufferLength, short @ StringLength2PtrDeclare short SQLDisconnect In odbc32 As _SQLDisconnect long ConnectionHandleDeclare short SQLFreeHandle In odbc32 long HandleType, Long Handle * Allocate the environment handle   hSQLEnv = 0lnResult = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, @ hSQLEnv)If (lnResult == SQL_SUCCESS Or lnResult == SQL_SUCCESS_WITH_INFO)    * Set the environment attribute to SQL_OV_ODBC3    lnResult = SQLSetEnvAttr(hSQLEnv, SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC3, 0)    If (lnResult == SQL_SUCCESS Or lnResult == SQL_SUCCESS_WITH_INFO)        * Allocate a connection handle        hSQLHdbc = 0        lnResult = SQLAllocHandle(SQL_HANDLE_DBC, hSQLEnv, @ hSQLHdbc)        If (lnResult == SQL_SUCCESS Or lnResult == SQL_SUCCESS_WITH_INFO)            szConnStrOut = SPACE(2048)            lnLen = 0            * Call SQLBrowseConnect for additional information               lnResult = SQLBrowseConnect(hSQLHdbc, lcDrvName, Len(m.lcDrvName), @ szConnStrOut, 2048, @ lnLen)            If (lnResult == SQL_SUCCESS Or lnResult == SQL_NEED_DATA)                 Return ListSqlServersList(Left(szConnStrOut, lnLen))            Endif            _SQLDisconnect(hSQLHdbc)           Endif           SQLFreeHandle(SQL_HANDLE_DBC, hSQLHdbc)       Endif   Endif   SQLFreeHandle(SQL_HANDLE_ENV, hSQLEnv)Clear DllsEndfunc Function ListSqlServersList(tcConnectString)       Local ii, lcSvrs, lcSvr, lcMsg, laSvrs[1]       m.nCnt = Alines(laSvrs, Strextract(tcConnectString, '{', '}'), .T., ',')    If m.nCnt > 0        lcMsg = '找到 '+TRANSFORM(m.nCnt)+'个 SQL Server 实例:'        For m.ii = 1 To m.nCnt            lcMsg = lcMsg + Chr(13) + Chr(10) + Chr(9) + Transform(m.ii) + laSvrs[m.ii]           Endfor       Else        lcMsg = '没有找到可用的 SQL Server 实例。'    Endif    Return lcMsgEndfunc


阅读全文(3543) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.016 second(s), page refreshed 144295882 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号