SQL CLR集成的多线程处理

iaxk 2009-11-25 04:04:40
正在利用SQL CLR集成用C#写一个SQL Server的存贮过程. 在程序中,我需要使用多线程, 但只要我在程序中调用了Thread.Start(), 在执行这个存贮过程时, SQL Server就会报如下的错误:
A .NET Framework error occurred during execution of user-defined routine or aggregate "CalcReport":
System.Security.HostProtectionException: Attempted to perform an operation that was forbidden by the CLR host.

The protected resources (only available with full trust) were: All
The demanded resources were: Synchronization, ExternalThreading

System.Security.HostProtectionException:
............

另外,我这个Dll装载到SQL Server中用的安全性是SAFE

看了半天的MSDN, 网上也搜了很久, 都没有搜到解决办法. 望高手指教, 在此多谢!
...全文
179 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
悔说话的哑巴 2009-11-25
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Data.SqlClient;

public partial class StoredProcedures
{
 [Microsoft.SqlServer.Server.SqlProcedure]

 public static void GetAuthors()
 {
  SqlPipe sp = SqlContext.Pipe;
  using (SqlConnection conn = new SqlConnection("context connection=true"))
  {
   conn.Open();
   SqlCommand cmd = new SqlCommand();
   cmd.CommandType = CommandType.Text;
   cmd.Connection = conn;
   cmd.CommandText = "Select DatePart(second, GetDate()) " + " As timestamp,* from authors";
   SqlDataReader rdr = cmd.ExecuteReader();
   sp.Send(rdr);
  }
 }

 [SqlProcedure]

 public static void GetTitlesByAuthor(string authorID)
 {
  string sql = "select T.title, T.price, T.type, " + "T.pubdate from authors A" +
     " inner join titleauthor TA on A.au_id = TA.au_id " +
     " inner join titles T on TA.title_id = T.title_id " +
     " where A.au_id = ’" + @authorID + "’";
  using (SqlConnection conn = new SqlConnection("context connection=true"))
  {
   conn.Open();
   SqlPipe sp = SqlContext.Pipe;
   SqlCommand cmd = new SqlCommand();
   cmd.CommandType = CommandType.Text;
   cmd.Connection = conn;
   cmd.CommandText = sql;
   SqlParameter paramauthorID = new SqlParameter("@authorID", SqlDbType.VarChar, 11);
   paramauthorID.Direction = ParameterDirection.Input;
   paramauthorID.Value = authorID;
   cmd.Parameters.Add(paramauthorID);
   SqlDataReader rdr = cmd.ExecuteReader();
   sp.Send(rdr);
  }
 }
}
a296694166 2009-11-25
  • 打赏
  • 举报
回复
jf
Dobzhansky 2009-11-25
  • 打赏
  • 举报
回复
Microsoft.Press.Customizing.the.Microsoft.dot.NET.Framework.Common.Language.Runtime.Feb.2005.eBook-LiB.chm

第十四章:

[Quote=Summary]
...
To integrate the CLR with SQL Server 2005 so that scalability isn't hindered, all schedulingrelated activities must be managed directly by SQL Server. The CLR hosting API provides an abstraction called the task manager that hosts can use to supply the CLR with the basic primitives it needs to create and manage how tasks behave in the process. By using the primitives supplied by the host instead of going directly to the operating system, the CLR makes the host (SQL Server 2005, in this case) aware of all threading-related activities that occur in the process such that the execution of managed tasks can be integrated with the host's custom scheduler.
...
[/Quote]
Dobzhansky 2009-11-25
  • 打赏
  • 举报
回复
CLR Host 微软给了 3 个:

1. 默认 host,
2. IE Host
3. Sql Server Host,

sqlserver 那个用的 纤程(fiber), 可能内部不允许启动线程.
wuyq11 2009-11-25
  • 打赏
  • 举报
回复
System.Security.HostProtectionException:试图执行的是由CLR主机禁止运行。
单独执行
参考
qldsrx 2009-11-25
  • 打赏
  • 举报
回复
也许SQL CLR不支持线程控制,如果是那样的话,你就不要使用多线程了,因为SQL本身是对一个连接一个线程的,为的是能够处理事务。
soaringbird 2009-11-25
  • 打赏
  • 举报
回复
有什么必要在存储过程里用线程呢?
iaxk 2009-11-25
  • 打赏
  • 举报
回复
自已顶

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧