简体   繁体   English

什么相当于Linq中的“SELECT SYSTEM_USER”

[英]What's the equivalent of “SELECT SYSTEM_USER” in Linq

I can get the identity of the currently logged in SQL Server user by using the SYSTEM_USER value, but is there a way to do this via somehow? 我可以使用SYSTEM_USER值获取当前登录的SQL Server用户的SYSTEM_USER ,但有没有办法通过以某种方式执行此操作? I'm looking for something like the below: 我正在寻找类似下面的内容:

var query = from u in dataContext.Users
            where u.domainname == dataContext.system_user
            select u.fullname;

Using something like Environment.UserName won't work in the event a non-domain user authenticates via VPN, and querying via a separate statement is undesirable because I'd like to use just one statement to get the data. 使用像Environment.UserName这样的东西在非域用户通过VPN进行身份验证的情况下不起作用,并且通过单独的语句进行查询是不可取的,因为我只想使用一个语句来获取数据。

Calling system functions can be done by creating new functions in the database which can be mapped further in the ORMs. 调用系统函数可以通过在数据库中创建新函数来完成,这些函数可以在ORM中进一步映射。 It depends then on the ORM capabilities regarding to mapping user database functions. 这取决于有关映射用户数据库功能的ORM功能。

CREATE FUNCTION dbo.fnSystemUser () returns varchar(120)
AS
BEGIN
    return (select SYSTEM_USER)
END

LINQ2SQL and EF will create a single SQL statement for the following LINQ query. LINQ2SQL和EF将为以下LINQ查询创建单个SQL语句。

var query = from u in dataContext.Users
            where u.domainname == dataContext.fnSystemUser()
            select u.fullname;

LINQ itself knows nothing about your database so no. LINQ本身对您的数据库一无所知,所以没有。 If you're talking about LINQ 2 Entities (as some keywords in your code sample imply) you may be able to use one of the ExecuteQuery overloads (or one of its relatives) with your explicit SQL statement. 如果您正在谈论LINQ 2实体(正如您的代码示例中的某些关键字所暗示的那样),您可以使用其中一个ExecuteQuery重载(或其中一个亲戚)与您的显式SQL语句。

As an alternative, you could parse the connection string to get the user's log on details. 作为替代方案,您可以解析连接字符串以获取用户登录详细信息。 I believe the SqlConnectionStringBuilder class should be able to tell you the user name and password of the user used to connect to the database if you aren't using Windows auth. 我相信如果您不使用Windows身份验证, SqlConnectionStringBuilder类应该能够告诉您用于连接数据库的用户的用户名和密码。

Try this: 试试这个:

    class System
    {
        public string SystemUser { get; set; }
    }

   var query = dataContext.ExecuteQuery<System>("select SYSTEM_USER AS 'SystemUser'");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM