我有一个用SQLCLR(SQL2005,.NET3.5)编写的触发器,我需要获取用户名、主机名和应用程序名。在TSQL中,我只使用
select suser_name() select host_name() select APP_NAME()
在我的.NET代码中,我尝试了
System.Security.Principal.WindowsIdentity.GetCurrent().Name System.Environment.MachineName System.AppDomain.CurrentDomain.ApplicationIdentity.FullName
但是在安全模式下运行时,我无法访问这些属性中的任何一个。有什么关于如何完成这个的想法吗?
谢谢
你可以使用 context connection 并执行必要的TSQL来检索所需的值。
在CLR触发器定义中:
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();