2

dxStatusbar1.Panels 1 .Text := DataModule2.UniConnectDialog1.Connection.Username;

...给我连接到 sql server 的用户名。但是,连接的用户在实际数据库中具有不同的名称。

示例:他的 sql server 登录名是“John”,用户映射到“Northwind”数据库。然而,在“Northwind”数据库中,他被称为“John Smith”。这就是他连接后我试图在 dxStatusbar1.Panels 1 .Text 中显示的名字(约翰史密斯)。

我怎么能得到那个?

编辑:尝试维多利亚建议:

UserName := DataModule2.UniConnection1.ExecSQL('SELECT :Result = CURRENT_USER', ['Result']);
 dxStatusbar1.Panels[1].Text := UserName; 

但得到:

在此处输入图像描述

4

2 回答 2

2

我找不到任何UniDAC API方法来获取当前连接的用户名(即使是 SDAC 也没有),所以我只需发出一个查询CURRENT_USER的 SQL 命令并从结果中获取名称:

SELECT CURRENT_USER;

或者使用USER函数以统一 SQL方式:

SELECT {fn USER};

由于您在评论中提到了存储过程,在我看来,您可能想直接从连接对象获取此信息而不使用查询对象。如果是这样,您甚至不需要存储过程,而是直接执行如下命令:

var
  UserName: string;
begin
  UserName := UniConnection1.ExecSQL('SELECT :Result = CURRENT_USER', ['Result']);
  ...
end;

或以统一的方式:

var
  UserName: string;
begin
  UserName := UniConnection1.ExecSQL('SELECT :Result = {fn USER}', ['Result']);
  ...
end;
于 2018-04-24T06:44:54.103 回答
0

其中之一可能会为您完成这项工作。没有测试过。

SELECT ORIGINAL_LOGIN()
SELECT SYSTEM_USER
SELECT SUSER_SNAME()

希望能帮助到你。

ORIGINAL_LOGIN:Returns the name of the login that connected to the instance of SQL Server. You can use this function to return the identity of the original login in sessions in which there are many explicit or implicit context switches.

系统用户:Allows a system-supplied value for the current login to be inserted into a table when no default value is specified.

SUSER_SNAME:Returns the login name associated with a security identification number (SID).

于 2018-04-24T06:29:21.630 回答