views:

21

answers:

1

I have a trigger written in sql clr (sql 2005, .net 3.5) and I need to get at username, hostname and app_name. In TSQL I would simply use

select suser_name()
select host_name()
select APP_NAME()

In my .Net code I tried

System.Security.Principal.WindowsIdentity.GetCurrent().Name
System.Environment.MachineName
System.AppDomain.CurrentDomain.ApplicationIdentity.FullName

but running in safe mode I do not have access to any of these properties. Any ideas on how to acomplish this?

Thanks

+1  A: 

You could use a context connection and execute the necessary TSQL to retrieve the values you need.

In your CLR trigger definition:

SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Context Connection=true";

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;

cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT suser_name() as suser_name, host_name() as host_name, APP_NAME() as app_name";

conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
Ed Harper
@ED yes I thought of that but I then need to use those values in a stored procedure call. I'm trying to minimize my calls through context connection. This is a definite possibility though and what I may have to end up doing.
Gratzy