2011-11-25 84 views
2

我想从SQL Server 2008使用GSM调制解调器的AT命令发送短信,所以我写了一个Visual C#SQL CLR数据库项目和一个存储过程。从SQL Server 2008 R2发送带GSM调制解调器的SMS

但是,当我执行该存储过程,我收到此错误:

Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

存储过程的源代码和发送短信的类是这些:

[Microsoft.SqlServer.Server.SqlProcedure] 
public static void SendSMS2(string phone, string message) 
{ 
    SMSManagement.SM2S ss = new SMSManagement.SM2S(); 
    ss.SendMessage(phone, message); 
} 

public void SendMessage(string PhoneNumber, string Message) 
{ 
    try 
    { 
     SerialPort port = new SerialPort(); 

     port.PortName = "COM5";     
     port.BaudRate = 115200;     
     port.DataBits = 8;     
     port.StopBits = StopBits.One;  
     port.Parity = Parity.None;   
     port.ReadTimeout = 300;   
     port.WriteTimeout = 300;   

     // Error occurred in here. 
     port.Open(); 
     port.DtrEnable = true; 
     port.RtsEnable = true; 

     SMS sms = new SMS(); 
     sms.Direction = SMSDirection.Submited; 
     sms.PhoneNumber = PhoneNumber; 
     sms.ValidityPeriod = new TimeSpan(4, 0, 0, 0); 
     sms.Message = Message; 

     Message = sms.Compose(SMS.SMSEncoding.UCS2); 

     ExecCommand(port, "AT", 300, "No phone connected"); 
     ExecCommand(port, "AT+CMGF=0", 300, "Failed to set pdu format."); 
     ExecCommand(port, "AT+CMGS=1", 300, "Failed to set message length."); 
     string command = Message + char.ConvertFromUtf32(26); 
     ExecCommand(port, command, 6000, "Failed to send message"); 

     port.Close(); 
    } 
    catch (Exception ex) 
    { 
     SqlContext.Pipe.Send(ex.Message); 
    } 
} 

的Visual Studio版本而SQL Server是2010 SP1和2008 R2。 CLR项目框架是2.0。

+0

你是如何将CLR程序集安装到SQL Server中的?你使用了什么命令和什么设置? –

+0

我将它部署到SQL Server中的我的数据库中,并将它显示在“程序集”列表中。 – ABI

+0

是的 - 当然 - 但是**如何部署它?直接从Visual Studio ??然后查看我的答案并检查您的设置。或者你用T-SQL脚本部署它('CREATE ASSEMBLY ......') - 然后请向我们展示该脚本! –

回答

1

你没有提到你如何将你的程序集安装到SQL Server中。如果你直接从Visual Studio部署 - 你指定了什么权限级别?

enter image description here

默认值是Safe,这是好的,只要你只在SQL Server中的工作 - 正则表达式匹配或类似的东西。

如果你想从SQL Server来“伸手”,并与世界沟通,你必须设置Permission LevelUnsafe您部署组装到SQL Server之前。

+0

我改变了它,但是在部署时我收到了这个错误:部署错误SQL01268:.Net SqlClient数据提供程序:消息10327,级别14,状态1,行1创建汇编'SMSManagement'的汇编失败,因为汇编'SMSManagement'未被授权对于PERMISSION_SET =不安全。如果满足以下任一条件,则会授权程序集:数据库所有者(DBO)具有UNSAFE ASSEMBLY权限,并且数据库具有TRUSTWORTHY数据库属性;或者该程序集使用证书或非对称密钥进行签名,该证书具有与UNSAFE ASSEMBLY权限相对应的登录名。 – ABI

+0

@ABI:你是否检查过这些要求?您的数据库所有者是否具有所需的权限?数据库是否有'TRUSTWORTHY'标志设置? –

+1

我设置了TRUSTWORTHY标志,但收到“主数据库中记录的数据库所有者SID不同于...”的错误。我修复了这个错误。现在工作很好。谢谢。 – ABI