【问题标题】:How can I get the current user from Sqlconnection如何从 Sqlconnection 获取当前用户
【发布时间】:2014-05-28 13:49:51
【问题描述】:

我正在尝试确定我的应用程序正在使用哪个用户对我的数据库进行身份验证,有没有办法让我可以获取用于执行 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());
                            }
                        }
                    }

【问题讨论】:

    标签: asp.net sqldatasource sqlconnection sqldataadapter


    【解决方案1】:

    试试这个:

    SELECT CURRENT_USER;
    GO
    

    上面返回的是当前用户的名字。检查here

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

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

    【讨论】:

      【解决方案2】:

      对数据库执行以下命令

      sp_who2
      

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

      【讨论】:

        猜你喜欢
        • 2018-12-05
        • 1970-01-01
        • 2018-01-10
        • 2020-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多