我如何知道我的程序集是否在SQL Server CLR环境中启动? [英] How do I know if my assembly is launched within SQL Server CLR environment?

查看:91
本文介绍了我如何知道我的程序集是否在SQL Server CLR环境中启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的Asp.Net项目和SQL CLR中使用相同的dll。当我的程序集由Asp.Net项目启动时,我的dll使用config-file来获取设置。在SQL Clr中启动我的程序集应该从特殊的config-table获取设置。我不知道如何找出,从哪里获取设置而不捕获异常。有没有更好的方法知道在SQL CLR中启动了dll?



I need to use the same dll in my Asp.Net project and in SQL CLR. When my assembly is launched by Asp.Net project my dll uses config-file to get settings. Being launched inside SQL Clr my assembly should get settings from special config-table. I don't know how to find out, from where to get settings without catching exceptions. Is there any better way to know that dll was launched within SQL CLR?

try
{
     //Check for Clr-specific connection
     using (SqlConnection connection = new SqlConnection("context connection=true"))
     {
         return "get settings from sql table";        
     }
}
catch (Exception ex)
{
    return "get settings from config-file";
}

推荐答案

使用 SqlContext.IsAvailable 属性 [ ^ ]:

Use the SqlContext.IsAvailable property[^]:
if (Microsoft.SqlServer.Server.SqlContext.IsAvailable)
{
    // Load the settings from the SQL table
}
else
{
    // Load the settings from the config file
}


这篇关于我如何知道我的程序集是否在SQL Server CLR环境中启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆