Dave Albiston wrote
Quote
>I am accessing a SQL Server 6.5 database using ODBC. At runtime, I need to
>find out the name of the NT server on which the database is running.
>Any ideas how to do this?
Here is a ODBC way of getting it.
type
PSQLCHAR = ^SQLCHAR;
SQLCHAR = Char;
SQLSCHAR = Char;
SQLINTEGER = Longint;
SQLUINTEGER = Cardinal;
SQLNUMERIC = Byte;
SQLPOINTER = Pointer;
SQLREAL = Single;
SQLSMALLINT = Smallint;
SQLUSMALLINT = Word;
{ function return type }
type
SQLRETURN = SQLSMALLINT;
{ generic data structures }
type
SQLHANDLE = Pointer;
SQLHENV = SQLHANDLE;
SQLHDBC = SQLHANDLE;
SQLHSTMT = SQLHANDLE;
SQLHDESC = SQLHANDLE;
const
SQL_HANDLE_ENV = 1;
SQL_HANDLE_DBC = 2;
SQL_HANDLE_STMT = 3;
SQL_HANDLE_DESC = 4;
function SQLAllocEnv(var EnvironmentHandle: SQLHENV):
SQLRETURN;stdcall;external 'odbc32.dll';
function SQLFreeEnv(var EnvironmentHandle: SQLHENV):
SQLRETURN;stdcall;external 'odbc32.dll';
function SQLGetInfo(ConnectionHandle: SQLHDBC;
InfoType: SQLUSMALLINT;
InfoValue: SQLPOINTER;
BufferLength: SQLSMALLINT;
var StringLength: SQLSMALLINT):
SQLRETURN;stdcall;external 'odbc32.dll';
function SQLConnect(ConnectionHandle: SQLHDBC;
ServerName: PSQLCHAR;
NameLength1: SQLSMALLINT;
UserName: PSQLCHAR;
NameLength2: SQLSMALLINT;
Authentication: PSQLCHAR;
NameLength3: SQLSMALLINT): SQLRETURN;stdcall;external
'odbc32.dll';
function SQLAllocConnect(EnvironmentHandle: SQLHENV;
var ConnectionHandle: SQLHDBC):
SQLRETURN;stdcall;external 'odbc32.dll';
function SQLDisconnect(ConnectionHandle: SQLHDBC):
SQLRETURN;stdcall;external 'odbc32.dll';
function SQLFreeHandle(HandleType: SQLSMALLINT;
Handle: SQLHANDLE): SQLRETURN;stdcall;external
'odbc32.dll';
procedure TForm1.Button1Click(Sender: TObject);
const
SQL_SERVER_NAME = 13;
var
GlblHEnv : SQLHENV;
HDBC : SQLHDBC;
Status : SQLRETURN;
ErrorCode : SQLINTEGER;
MsgLen : SQLSMALLINT;
Msg : array[0..512] of SQLCHAR;
DSN, User, Pswd : array[0..32] of SQLCHAR;
begin
GlblHEnv := nil;
if SQLAllocEnv(GlblHEnv) <> 0 then Exit;
Status := SQLAllocConnect(GlblHEnv, HDBC);
FillChar(Msg[0], 512, #0);
if Status = 0 then
begin
DSN := 'PUBSDSN'; User := 'sa'; Pswd := '';
SQLConnect(HDBC, @DSN, 32, @User, 32, @pswd, 32);
Status := SQLGetInfo(HDBC, SQL_SERVER_NAME, @Msg[0], 512, MsgLen);
SQLDisconnect(HDBC);
end;
SQLFreeHandle(SQL_HANDLE_DBC, HDBC);
SQLFreeHandle(SQL_HANDLE_ENV, GlblHEnv);
Edit1.Text := string(Msg);
end;
If you want to use it over the BDE, you need to get the HDBC from BDE using
DbiGetNativeHandle etc.( Check BDE documentation.) And then use this HDBC in
the SQLGetInfo function.
--Reddy.