Home > database >  VFP SQL, find the current available SQL server, network always display an error: not found available
VFP SQL, find the current available SQL server, network always display an error: not found available

Time:10-07

Thisform. CboStep. AddItem (" step 1 - select to connect to the database server ")
Thisform. CboStep. AddItem (\ "step 2 - specifies the login user and password, and install the database")
Thisform. CboStep. AddItem (\ "step 3 - the complete")

The LOCAL oSQLDMO AS SQLDMO. Application
OSQLDMO=CREATEOBJECT (" SQLDMO. Application ") & amp; & Establish SQLDMO object
OServerNameList=oSQLDMO. ListAvailableSQLServers ()

IF oServerNameList. Count=0 & amp; & Not found the available server
Thisform. PgfStep. Page1. Optiongroup1. Option1. Enabled=. F.
Thisform. PgfStep. Page1. CboServer. Enabled=. F.
Thisform.pgfStep.Page1.Optiongroup1.Value=https://bbs.csdn.net/topics/2
Thisform. PgfStep. Page1. LblStep1. Caption="not found available SQL Server Server, you can be in" +;
"In the text box below to enter a database server name or IP address,"
Thisform. PgfStep. Page1. TxtServer. SetFocus
The ELSE
Thisform. PgfStep. Page1. TxtServer. Enabled=. F.
FOR I=1 TO oServerNameList. Count & amp; & The server name is added to the drop-down list boxes
Thisform. PgfStep. Page1. CboServer. AddItem (oServerNameList. Item (I))
ENDFOR
Thisform.pgfStep.Page1.cboServer.Value=https://bbs.csdn.net/topics/oServerNameList.Item (1)
Thisform. PgfStep. Page1. LblStep1. Caption="you can directly from the drop-down boxes below directly choose" +;
"A server name, if you need the server has not been included in the list, you can directly in the following article" +;
"This box manual input,"
Thisform. PgfStep. Page1. CboServer. SetFocus
ENDIF

CodePudding user response:

 enumerated all instances of SQL Server Server name in the local area network (LAN) 

* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
By: DKFDTF
Source: http://blog.csdn.net/dkfdtf/archive/2009/02/02/3859150.aspx

If you want to check whether there is the name of a specified SQL Server Server, or want to give the user a to choose their own needs to connect SQL Server, you need to enumerate all the instances of SQL Server Server name within a local area network (LAN), I know there are three kinds of methods:

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

1. Use object SQLDMO ListAvailableSQLServers method
LoSqlDMO=Createobject (" SQLDMO. Application ")
LoSvrs=loSqlDMO. ListAvailableSQLServers ()
LnSvrsCount=loSvrs. Count
If lnSvrsCount=0
?" Could not find the available SQL Server Server, "
The Else
?" Can connect the SQL Server Server: "+ Transform (lnSvrsCount) +", respectively, "
For Each lcSvr loSvrs In
? LcSvr
Endfor
Endif
Release loSqlDMO

This method is simple, the function is complete, you can enumerate the default and non-default instance name all of the SQL Server instance, the disadvantage is that running the code must be installed on the machine of sqldmo object component, if the machine is installed on the SQL Server, or you to install the components of natural no problem, if it is a common client, however, it only need to connect to the SQL Server data, or you just want to check before installing SQL Server installation environment, may be because there is no this component to achieve a goal,

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

2. Use the API function NetServerEnum enumeration specifies the type of server

# Define MAX_PREFERRED_LENGTH - 1
# Define SV_TYPE_SQLSERVER 0 x00000004

Declare Long NetServerEnum netapi32 In;
String ServerName, Long nlevel, Long @ bufptr, Long prefmaxlen,;
Long @ entriesread, long @ totalentries, long servertype,;
String domain, Long resume_handle
Declare Long NetApiBufferFree netapi32 In Long bufptr
Declare Long lstrlenW win32api In String @ lpString

Store 0 To lnBuff lnReadNums, lnTotNums
If 0==NetServerEnum (Null, 100, @ lnBuff MAX_PREFERRED_LENGTH,;
@ lnReadNums, @ lnTotNums SV_TYPE_SQLSERVER, Null, 0);
And lnReadNums & gt; 0
? 'find instances of SQL Server:'
For 2=1 To lnReadNums
M.l nAddr=CToBin (Sys (2600, m.l nBuff + (m.i I - 1) * 8 + 4, 4), 'rs')
M.l cSrv=Sys (2600, m.l nAddr, 256)
? Left (Strconv (m.l cSrv, 6), lstrlenW (m.l cSrv))
Endfor
The Else
? 'not found available SQL Server instance,'
Endif
If lnBuff & gt; 0
NetApiBufferFree (lnBuff)
Endif
This is just a simple code, without any error handling, the API function can also detect all available SQL Server Server (service), advantage is fast, do not need to install any additional components, defect is unable to list the default installation instance name, and sometimes the SQL service starts it take long to detect,

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

3. The enumeration of the specified type using ODBC API server

# 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}
LcLookUpKey='SERVER: SERVER='

Declare short SQLAllocHandle odbc32 In;
Short HandleType, Long InputHandle, Long @ OutputHandlePtr
Declare short SQLSetEnvAttr odbc32 In;
Long EnvironmentHandle, long nAttribute, long ValuePtr, long StringLength
Declare short SQLBrowseConnect odbc32 In;
Long ConnectionHandle, String InConnectionString, short StringLength1,;
String @ OutConnectionString, short BufferLength, short @ StringLength2Ptr
Declare short SQLDisconnect In odbc32 As _SQLDisconnect;
Long ConnectionHandle
Declare short SQLFreeHandle odbc32 In;
Long HandleType, long Handle

* the Allocate the environment handle
HSQLEnv=0
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • VFP