Add(pCurSvrInfo^.sv100_name);
Inc(i);
Inc(pCurSvrInfo);
end;
end;
finally
if Assigned(pReturnSvrInfo) then
NetApiBufferFree(pReturnSvrInfo);
end;
except
Result := False;
end;
end;
三、 SQLBrowseConnect
描述:ODBC函数(Microsoft Open Database Connectivity,开放式数据库连接),存在于odbc32.dll文件中;通过SQLBrowseConnect函数可返回连接字符串信息,包括DSN、DRIVER、SERVER、UID、PWD、APP、WSID、DATABASE、LANGUAGE等信息。在函数GetODBCInfo 中传入itServer、itDatabase、itLanguage可分别取得“服务器”、“数据库”及“语言”等信息列表,其中itDatabase、itLanguage默认取本地信息,取远程信息请自行修改“''Driver={SQL Server};SERVER=(local);UID=sa;PWD=''”连接字符串。
列表类型:列举装有“客户端”和“服务端”的计算机。
适用条件:由于MDAC 2.6 、2.6 SP1、2.7和Microsoft ODBC Driver for SQL Server 2000 2000.80.194有Bug,因此在这些版本中此函数无法取得Microsoft SQL Server 7.0的服务器。
速度:中
调用示例:GetODBCInfo(ListBox1.items, itServer);
代码:
type
TInfoType = (itServer, itDatabase, itLanguage);
SQLHANDLE = Pointer;
SQLSMALLINT = SHORT;
SQLINTEGER = LongInt;
PSQLHANDLE = ^SQLHANDLE;
SQLHENV = SQLHANDLE;
SQLHDBC = SQLHANDLE;
SQLRETURN = SQLSMALLINT;
SQLCHAR = UCHAR;
PSQLCHAR = ^SQLCHAR;
SQLPOINTER = Pointer;
PSQLSMALLINT = ^SQLSMALLINT;
function SQLAllocHandle(HandleType: SQLSMALLINT; InputHandle: SQLHANDLE;
OutputHandle: PSQLHANDLE): SQLRETURN; stdcall; external ''odbc32.dll'' name
''SQLAllocHandle'';
function SQLSetEnvAttr(EnvironmentHandle: SQLHENV; Attribute: SQLINTEGER;
Value: SQLPOINTER; StringLength: SQLINTEGER): SQLRETURN; stdcall; external
''odbc32.dll'' name ''SQLSetEnvAttr'';
function SQLBrowseConnect(hdbc: SQLHDBC; szConnStrIn: PSQLCHAR;
cbConnStrIn: SQLSMALLINT; szConnStrOut: PSQLCHAR;
cbConnStrOutMax: SQLSMALLINT; pcbConnStrOut: PSQLSMALLINT): SQLRETURN;
stdcall; external ''odbc32.dll'' name ''SQLBrowseConnect'';
function SQLDisconnect(ConnectionHandle: SQLHDBC): SQLRETURN; stdcall; external
''odbc32.dll'' name ''SQLDisconnect'';
function SQLFreeHandle(HandleType: SQLSMALLINT; Handle: SQLHANDLE): SQLRETURN;
stdcall; external ''odbc32.dll'' name ''SQLFreeHandle'';
const
SQL_HANDLE_ENV = 1;
SQL_HANDLE_DBC = 2;
SQL_NULL_HANDLE = LongInt(0);
SQL_SUCCESS = 0;
SQL_ERROR = -1;
SQL_ATTR_ODBC_VERSION = 200;
SQL_OV_ODBC3 = ULONG(3);
SQL_NTS = -3;
function GetODBCInfo(var AList: TStrings; InfoType: TInfoType): Boolean;
const
ConnStrOutMax = 4824;
SplitterStr = ''={'';
var
HENV: SQLHENV;
HDBC: SQLHDBC;
RetCode: SQLRETURN;
ConnStrOut: PSQLCHAR;
cbConnStrOut: SQLSMALLINT;
ConnStrIn, TmpStr: string;
TmpPos: Integer;
begin
case InfoType of
itServer: ConnStrIn := ''Driver={SQL Server}'';
itDatabase, itLanguage: ConnStrIn := ''Driver={SQL Server};SERVER=(local);UID=sa;PWD='';
end;
Result := False;
try
// 分配 ODBC 环境句柄
RetCode := SQLAllocHandle(SQL_HANDLE_ENV, SQLPOINTER(SQL_NULL_HANDLE), @HENV);
if RetCode = SQL_ERROR then
Exit;
// 设置 ODBC 版本
RetCode := SQLSetEnvAttr(HENV, SQL_ATTR_ODBC_VERSION, SQLPointer(SQL_OV_ODBC3), 0);
if RetCode <> SQL_SUCCESS then
Exit;
// 分配数据库连接句柄
RetCode := SQLAllocHandle(SQL_HANDLE_DBC, HENV, @HDBC);