Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to test for the existance of a SQL Server
Message
 
À
27/02/2007 15:50:03
Information générale
Forum:
Visual FoxPro
Catégorie:
Client/serveur
Versions des environnements
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows XP
Database:
MS SQL Server
Divers
Thread ID:
01199067
Message ID:
01199325
Vues:
20
>OK, I guess now that I think of it, what I also need is a function that tells me if I can "see" the server right now, before I attempt to connect to it, so I can write code to warn the user that either they are not "on" the network or the server PC is "not available".
>
>I tried a simple function in FoxPro, ADIR() like this:
>
>m.nfiles = ADIR(aTempArray,"\\SQLSERVER01") but this doesn't work. Is there another approach that works?
>
>(I tried ANETRESOURCES() but realized that it wouldn't help if no "shares" where created on the SQL Server) and it may to tell me if the server was available...
>
>Thanks!!
>
>
>>>Is there a function or something that I can use to test for the existence of a SQL Server?
>>>
>>>For example, is there something like this:
>>>
>>>m.ifServerExist = SOMEFUNCTION("ABC_SQL01")
>>>
>>>&& Where ABC_SQL01 = the DNS name or IP address of the server.
>>>
>>>Thanks!!
>>
>>Check List of SQL Servers Thread #779753.

Try:
CREATE CURSOR csResult (PlatformId varchar(50), ServerName varchar(150), MajorVer int,;
                        MinorVer Int, SoftType int, ServerMemo vachar(100))
EnumerateServers()
BROWSE NORMAL

FUNCTION EnumerateServers()
    DECLARE INTEGER NetServerEnum IN netapi32;
        INTEGER servername, INTEGER level, INTEGER @ bufptr,;
        INTEGER prefmaxlen, INTEGER @ entriesread, INTEGER @ totalentries,;
        INTEGER servertype, INTEGER domain, INTEGER resume_handle

    DECLARE INTEGER NetApiBufferFree IN netapi32 INTEGER Buffer

    DECLARE RtlMoveMemory IN kernel32 As Heap2String;
        STRING @ Destination, INTEGER Source, INTEGER nLength 


* Specifies the type of software the computer is running
    #DEFINE SV_TYPE_WORKSTATION 1
    #DEFINE SV_TYPE_SERVER 2
    #DEFINE SV_TYPE_SQLSERVER 4
    #DEFINE SV_TYPE_DOMAIN_CTRL  8
    #DEFINE SV_TYPE_PRINTQ_SERVER 0x200
    #DEFINE SV_TYPE_SERVER_UNIX 0x800
    #DEFINE SV_TYPE_SERVER_NT 0x8000
    #DEFINE SV_TYPE_DOMAIN_MASTER 0x80000
    #DEFINE SV_TYPE_WINDOWS 0x400000
    #DEFINE SV_TYPE_ALL 0xFFFFFFFF

    * testing various server lists
*    = EnumServers (SV_TYPE_PRINTQ_SERVER)
*    = EnumServers (SV_TYPE_SERVER)
    = EnumServers (SV_TYPE_SQLSERVER)
*    = EnumServers (SV_TYPE_SERVER_NT)
*    = EnumServers (SV_TYPE_DOMAIN_CTRL)
*    = EnumServers (SV_TYPE_ALL)
RETURN 

PROCEDURE  EnumServers (lnServerType)
    #DEFINE MAX_PREFERRED_LENGTH -1
    #DEFINE SINFO_101_SIZE       24

    *| typedef struct _SERVER_INFO_101 {
    *|   DWORD     sv101_platform_id;     4
    *|   LPWSTR    sv101_name;            4
    *|   DWORD     sv101_version_major;   4
    *|   DWORD     sv101_version_minor;   4
    *|   DWORD     sv101_type;            4
    *|   LPWSTR    sv101_comment;         4
    *| } SERVER_INFO_101, *PSERVER_INFO_101, *LPSERVER_INFO_101;

    LOCAL lnBuffer, lnCountRead, lnCountTotal, lnResult
    STORE 0 TO lnBuffer, lnCountRead, lnCountTotal

    WAIT WINDOW "Requesting data..." NOWAIT
    lnResult = NetServerEnum (0, 101, @lnBuffer, MAX_PREFERRED_LENGTH,;
        @lnCountRead, @lnCountTotal, lnServerType, 0, 0)
    WAIT CLEAR
    
    IF lnResult <> 0
       RETURN ""
    ELSE
        LOCAL lcBuffer, lnBufLen, lnEntry, lnPlatformId, lnNamePtr,;
            lnMajorVer, lnMinorVer, lnSofttype, lnMemoPtr,;
            lcServerName, lcServerMemo
    
        * copying the resulting array of SERVER_INFO_101 structures
        * to a VFP string
        lnBufLen = lnCountRead * SINFO_101_SIZE
        lcBuffer = Repli (Chr(0), lnBufLen)
        = Heap2String (@lcBuffer, lnBuffer, lnBufLen)

        * scanning resulting array
        FOR lnEntry = 1 TO lnCountRead
            lcServerInfo = SUBSTR (lcBuffer,;
                (lnEntry-1)*SINFO_101_SIZE+1, SINFO_101_SIZE)

            lnPlatformId = buf2dword (SUBSTR (lcServerInfo,  1,4))
            lnNamePtr    = buf2dword (SUBSTR (lcServerInfo,  5,4))
            lnMajorVer   = buf2dword (SUBSTR (lcServerInfo,  9,4))
            lnMinorVer   = buf2dword (SUBSTR (lcServerInfo, 13,4))
            lnSofttype   = buf2dword (SUBSTR (lcServerInfo, 17,4))
            lnMemoPtr    = buf2dword (SUBSTR (lcServerInfo, 21,4))

            lcServerName = getStrFromMem (lnNamePtr)
            lcServerMemo = getStrFromMem (lnMemoPtr)
            WAIT WINDOW lcServerName NOWAIT

            INSERT INTO csResult VALUES (lnPlatformId, lcServerName,;
                lnMajorVer, lnMinorVer, lnSofttype, lcServerMemo)
        ENDFOR
        WAIT CLEAR
    ENDIF
    
    * releasing the memory block allocated regardless of the result
    * by OS within NetServerEnum call
    = NetApiBufferFree (lnBuffer)
RETURN

FUNCTION getStrFromMem (lnMemBlock)
    * converting memory allocated Unicode string to a VFP string
    #DEFINE StrBufferLength   250
    LOCAL lcBuffer
    lcBuffer = SPACE(StrBufferLength)
    = Heap2String (@lcBuffer, lnMemBlock, StrBufferLength)
    lcBuffer = SUBSTR (lcBuffer, 1, AT(Chr(0)+Chr(0),lcBuffer)-1)

RETURN STRTRAN(lcBuffer, Chr(0),"")

FUNCTION buf2dword (lcBuffer)
RETURN Asc(SUBSTR(lcBuffer, 1,1)) + ;
       Asc(SUBSTR(lcBuffer, 2,1)) * 256 +;
       Asc(SUBSTR(lcBuffer, 3,1)) * 65536 +;
       Asc(SUBSTR(lcBuffer, 4,1)) * 16777216
Kill me I can't tell you from where I got that code (It is not mine at all). I am almost sure it is from here but who wrote it ...
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform