Board index » delphi » Getting database userid from MS SQL Server, 7.0 and higher

Getting database userid from MS SQL Server, 7.0 and higher

In MS SQL 6.5, I could just do the following:

select user

And I would get back a one-row result set in which the first field was the
userid for the current user, as known by the database server.  After setting
the SQL property of a TQuery to this, I could use code like:

Query1.Open;
Query1.First;
sUserID := Query1.Fields[0].AsString;

and get the value of the userid.  In SQL 7.0, I instead get , "List index
out of bounds" .
When I step through the code, the value of Query1.Fields.Count is zero.
Does anybody have a good technique for getting the userid from an MS SQL
Server database, and making it available as a string within Delphi, that
works with MS SQL 6.5 and 7.0?

I should mention that my SQL 7.0 is the "light" version and is running on a
Windows 2000 notebook.
I'm running Delphi 5.0x (current patches) and the version of BDE that comes
with it.

 

Re:Getting database userid from MS SQL Server, 7.0 and higher


Try this.

    Query1.sql.add('select convert(char(30),user) as currentuser';
    query1.open;
    user:=query1.fieldbyname('currentuser').asstring;

    User and current_user still works with SQL Server 7 but the datatype
must have changed to a new type which is not recognised by the BDE. With
ADO, it works without convert.
--
Sunil Furtado
New Mangalore Port Trust

Quote
Jim Daniels <[email protected]> wrote in message

news:[email protected]
Quote
> In MS SQL 6.5, I could just do the following:

> select user

> And I would get back a one-row result set in which the first field was the
> userid for the current user, as known by the database server.  After
setting
> the SQL property of a TQuery to this, I could use code like:

> Query1.Open;
> Query1.First;
> sUserID := Query1.Fields[0].AsString;

> and get the value of the userid.  In SQL 7.0, I instead get , "List index
> out of bounds" .
> When I step through the code, the value of Query1.Fields.Count is zero.
> Does anybody have a good technique for getting the userid from an MS SQL
> Server database, and making it available as a string within Delphi, that
> works with MS SQL 6.5 and 7.0?

> I should mention that my SQL 7.0 is the "light" version and is running on
a
> Windows 2000 notebook.
> I'm running Delphi 5.0x (current patches) and the version of BDE that
comes
> with it.

Other Threads