SQL Server 2008:执行尝试通过连接访问数据库的 CLR 函数时出现异常
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许尝试将建立连接的 using 语句更改为
Which 告诉 CLR 使用当前的 SQL 连接。
我猜测您失败的原因是,通过指定集成安全性,它将使用 SQL Server 服务帐户的凭据。
有关此内容的更多信息此处
Maybe try changing the using statement that establishes the connection to
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