¿Cómo puedo obtener el usuario actual de Sqlconnection?

I am trying to determine what user my application is using to authenticate to my database, is there a way that I can get the user that is being used to execute the sql code?

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());
                            }
                        }
                    }

preguntado el 28 de mayo de 14 a las 14:05

2 Respuestas

Prueba esto:

SELECT CURRENT_USER;
GO

The above returns the name of the current user. Check aquí

To see current users connected you could use sp_who

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

contestado el 28 de mayo de 14 a las 15:05

Execute following command against database

sp_who2

it lists all open connections including logins and host names.

contestado el 28 de mayo de 14 a las 15:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.