SQL Server 2008:执行尝试通过连接访问数据库的 CLR 函数时出现异常

发布于 2024-09-11 03:36:06 字数 1913 浏览 3 评论 0原文

我想在 SQL Server 2008 中使用 CLR 表值函数,它从内部访问数据库。我有一个许可例外。我正在尝试以创建该函数的同一用户身份执行该函数。所以问题的原因尚不清楚..

这是函数:

public partial class MyClass
    {    
        [SqlFunction(
            DataAccess = DataAccessKind.Read,
            FillRowMethodName = "Availability_FillRow",
            TableDefinition = "B0RID nchar(32)")]
        public static IEnumerable Fn_SEARCH_Availability(SqlDateTime checkin, SqlInt32 overnights)
        {
            if (checkin.IsNull || overnights.IsNull)
            {
                return null;
            }
            List<ResultRoom> roomsResultList = new List<ResultRoom>();


        using (SqlConnection conn = new SqlConnection("Data Source=MachineName;Initial Catalog=DBNANE;Integrated Security=True"))
        {
            conn.Open();
        }
        return roomsResultList;
    }
}

这是异常

A .NET Framework error occurred during execution of user-defined routine or aggregate "Fn_SEARCH_Availability": 
System.Security.SecurityException: Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
System.Security.SecurityException: 
   at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
   at System.Security.PermissionSet.Demand()
   at System.Data.Common.DbConnectionOptions.DemandPermission()
   at System.Data.SqlClient.SqlConnection.PermissionDemand()
   at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
   at StoredProcedures.Fn_SEARCH_Availability(SqlDateTime checkin, SqlInt32 overnights)

I want to use CLR table-valued function in SQL Server 2008, which accesses a database from inside itself. I've got a permission exception. I am trying to execute function as the same user as under which it was created. So cause of the problem is not clear..

Here is the function:

public partial class MyClass
    {    
        [SqlFunction(
            DataAccess = DataAccessKind.Read,
            FillRowMethodName = "Availability_FillRow",
            TableDefinition = "B0RID nchar(32)")]
        public static IEnumerable Fn_SEARCH_Availability(SqlDateTime checkin, SqlInt32 overnights)
        {
            if (checkin.IsNull || overnights.IsNull)
            {
                return null;
            }
            List<ResultRoom> roomsResultList = new List<ResultRoom>();


        using (SqlConnection conn = new SqlConnection("Data Source=MachineName;Initial Catalog=DBNANE;Integrated Security=True"))
        {
            conn.Open();
        }
        return roomsResultList;
    }
}

Here is the exception

A .NET Framework error occurred during execution of user-defined routine or aggregate "Fn_SEARCH_Availability": 
System.Security.SecurityException: Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
System.Security.SecurityException: 
   at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
   at System.Security.PermissionSet.Demand()
   at System.Data.Common.DbConnectionOptions.DemandPermission()
   at System.Data.SqlClient.SqlConnection.PermissionDemand()
   at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
   at StoredProcedures.Fn_SEARCH_Availability(SqlDateTime checkin, SqlInt32 overnights)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

許願樹丅啲祈禱 2024-09-18 03:36:06

也许尝试将建立连接的 using 语句更改为

using (SqlConnection conn = new SqlConnection("context connection=true;"))

Which 告诉 CLR 使用当前的 SQL 连接。

我猜测您失败的原因是,通过指定集成安全性,它将使用 SQL Server 服务帐户的凭据。

有关此内容的更多信息此处

Maybe try changing the using statement that establishes the connection to

using (SqlConnection conn = new SqlConnection("context connection=true;"))

Which tells the CLR to use the current SQL connection.

My guess for why yours failed is that by specifying Integrated security it would be using the credentials of the SQL Server Service account.

More info on this here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文