简体   繁体   English

SQLCLR存储过程数据类型

[英]SQLCLR Stored procedure datatypes

I decided to create a SQLCLR stored procedure to replace a SQL Server stored procedure due to it's complexity. 由于它的复杂性,我决定创建一个SQLCLR存储过程来替换SQL Server存储过程。

Question: is there any data type or API restrictions when we write CLR stored procedures? 问题:编写CLR存储过程时,是否有任何数据类型或API限制? If so, can someone give me any heads up on this. 如果是这样,有人可以给我任何建议。

I am planning to use var, List<T>, DataTable, DataRow[] and a Queue along with few LINQ extension methods. 我打算使用var, List<T>, DataTable, DataRow[]Queue以及一些LINQ扩展方法。

This is actually a fairly open-ended question as there are nuances in both what can be done in the .NET / CLR code itself and what datatypes can/should be used as input/output parameters. 实际上,这是一个相当开放的问题,因为.NET / CLR代码本身可以做什么以及可以/应该将哪些数据类型用作输入/输出参数都存在细微差别。

Starting with the basics, the nuances depend on what version of SQL Server you are using, and which particular question is being asked. 从基础开始,细微差别取决于您所使用的SQL Server版本以及所询问的特定问题。

.NET Framework Versions .NET Framework版本

  • Mixed-mode is not allowed: only a single CLR version can be used within the SQL Server CLR Host, and the CLR version depends on the version of SQL Server and cannot be changed 不允许使用混合模式: SQL Server CLR主机内只能使用一个CLR版本,并且CLR版本取决于SQL Server的版本,不能更改
  • SQL Server 2005, 2008, and 2008 R2: Statically bound to CLR 2.0 SQL Server 2005、2008和2008 R2:静态绑定到CLR 2.0
  • SQL Server 2012, 2014, and 2016: Statically bound to CLR 4.0 SQL Server 2012、2014和2016:静态绑定到CLR 4.0

.NET Framework Libraries and Data Type Mappings .NET Framework库和数据类型映射

SQL Server 2005 SQL Server 2005

Supported .NET Framework Libraries : 支持的.NET Framework库

  • CustomMarshalers CustomMarshalers
  • Microsoft.VisualBasic Microsoft.VisualBasic程序
  • Microsoft.VisualC Microsoft.VisualC
  • mscorlib mscorlib程序
  • System 系统
  • System.Configuration 系统配置
  • System.Data System.Data
  • System.Data.OracleClient System.Data.OracleClient的
  • System.Data.SqlXml System.Data.SqlXml
  • System.Deployment System.Deployment
  • System.Security System.Security
  • System.Transactions System.Transactions的
  • System.Web.Services System.Web.Services
  • System.Xml 的System.Xml

Data Types: 资料类型:

SQL Server 2008, 2008 R2, 2012, 2014, and 2016: SQL Server 2008、2008 R2、2012、2014和2016:

Supported .NET Framework Libraries : ( 2 libraries added as noted below ) 支持的.NET Framework库 :( 添加了两个库,如下所述

  • CustomMarshalers CustomMarshalers
  • Microsoft.VisualBasic Microsoft.VisualBasic程序
  • Microsoft.VisualC Microsoft.VisualC
  • mscorlib mscorlib程序
  • System 系统
  • System.Configuration 系统配置
  • System.Data System.Data
  • System.Data.OracleClient System.Data.OracleClient的
  • System.Data.SqlXml System.Data.SqlXml
  • System.Deployment System.Deployment
  • System.Security System.Security
  • System.Transactions System.Transactions的
  • System.Web.Services System.Web.Services
  • System.Xml 的System.Xml
  • System.Core.dll ( added in SQL Server 2008 ) System.Core.dll( 在SQL Server 2008中添加
  • System.Xml.Linq.dll ( added in SQL Server 2008 ) System.Xml.Linq.dll( 在SQL Server 2008中添加

Data Types: 资料类型:

Misc. 杂项。

  • Regarding Data Types: if there is a Sql* type available (eg SqlInt32 ) then use it; 关于数据类型:如果有可用的Sql*类型(例如SqlInt32 ),则使用它;否则,使用它。 don't use the .NET type in the C# method for input parameters (eg int or Int32 ). 不要在C#方法中使用.NET类型作为输入参数(例如intInt32 )。 Only use the native .NET type if there is no Sql* type for the SQL Server Data Type to map to. 仅当没有Sql*类型映射到SQL Server数据类型时,才使用本机.NET类型。 The two common instances of this exception are using object (which can be DbNull.Value ) to map to SQL_VARIANT , and DateTime / DateTime? 此异常的两个常见实例是使用object (可以是DbNull.Value )映射到SQL_VARIANT ,以及DateTime / DateTime? which map to DATETIME2 . 哪个映射到DATETIME2
  • You can reference / use 3rd party assemblies as well as Framework Libraries outside of those listed above, but you then need to import them with a PERMISSION_SET of UNSAFE into the database in which your code also resides. 您可以参考/使用第三方组件以及框架库上面列出的那些之外,但你需要用它们导入PERMISSION_SETUNSAFE进入你的代码还驻留的数据库。 You will also probably need to set the database to TRUSTWORTHY ON in order to set them to UNSAFE because it does not seem possible (not that I can find, anyway) to create the Asymmetric Key from Microsoft provided DLLs. 您可能还需要将数据库设置为TRUSTWORTHY ON ,以便将它们设置为UNSAFE因为从Microsoft提供的DLL中似乎不可能(无论如何,我也找不到)。
  • Generally speaking, anything written against .NET 2.0 such that it works in SQL Server 2005 should work in any of the later versions of SQL Server. 一般来说,任何针对.NET 2.0编写的,使其可以在SQL Server 2005中使用的东西都应在任何更高版本的SQL Server中都可以使用。 I have yet to come across something that works in .NET 2.0 on 2005 that doesn't work in any of the version to come after 2005. 我还没有遇到在2005年的.NET 2.0中可以使用的功能,但是在2005年以后的任何版本中都无法使用。
  • You cannot pass Table-Valued Parameters (TVPs) into a SQLCLR-based object (Stored Procedure, Function, etc.). 您不能将表值参数(TVP)传递到基于SQLCLR的对象(存储过程,函数等)中。 That point, as well as some other helpful info, is noted in the MSDN documentation for CLR Stored Procedures 有关CLR存储过程的MSDN文档中指出了这一点以及其他一些有用的信息。
  • I have not seen any issues when using List<T> , DataTable , and DataRow[] . 使用List<T>DataTableDataRow[]时,我没有看到任何问题。
  • I have not used Queue or var but would not expect any issues when using them. 我没有使用Queuevar但是使用它们时不会出现任何问题。
  • It has been years since I tried LINQ but seem to recall the only issue being that it was not included in the SQL Server 2005 set of supported Framework Libraries so that didn't work for me as I need to support 2005 - 2014, but for anyone who doesn't need to worry about SQL Server 2005 it should be ok. 自从我尝试LINQ已经有好几年了,但似乎回想起唯一的问题是它没有包含在SQL Server 2005支持的框架库集中,因此对我不起作用,因为我需要支持2005-2014,但是对于任何不需要担心SQL Server 2005的人都应该可以。
  • For more details on the topic of SQLCLR, I have a series of articles on SQL Server Central (free registration required): Stairway to SQLCLR 有关SQLCLR主题的更多详细信息,我在SQL Server Central中有一系列文章(需要免费注册): SQLCLR的阶梯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM