0

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

2 Answers 2

4

Try this:

SELECT CURRENT_USER;
GO

The above returns the name of the current user. Check here

To see current users connected you could use sp_who

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

Execute following command against database

sp_who2

it lists all open connections including logins and host names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.