繁体   English   中英

如何从Sqlconnection获取当前用户

[英]How can I get the current user from Sqlconnection

我正在尝试确定我的应用程序正在使用哪个用户来对数据库进行身份验证,有没有办法让我获得用于执行sql代码的用户?

string connection = WebConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;

using (SqlConnection conn = new SqlConnection(connection))
       {
        using (SqlDataAdapter adapter = new SqlDataAdapter())
               {
                   adapter.SelectCommand = new SqlCommand();
                   adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                   adapter.SelectCommand.CommandText = "MySPName";
                   adapter.SelectCommand.Connection = conn;
                   using (DataSet ds = new DataSet())
                        {
                            try
                            {
                                adapter.SelectCommand.Connection.Open();
                                logULS("Connection Open");
                                //Want to log the user that has the connection open...
                                adapter.Fill(ds);
                                adapter.SelectCommand.Connection.Close();

                              }
                            catch (Exception ex)
                            {
                                logULS(ex.ToString());
                            }
                        }
                    }

尝试这个:

SELECT CURRENT_USER;
GO

以上返回当前用户的名称。 在这里检查

若要查看当前连接的用户,可以使用sp_who

sp_who [ [ @loginame = ] 'login' | session ID | 'ACTIVE' ]

对数据库执行以下命令

sp_who2

它列出了所有打开的连接,包括登录名和主机名。

暂无
暂无

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

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