简体   繁体   English

SQL CLR和可空的datetime参数

[英]SQL CLR and nullable datetime parameter

I'm trying to write a SQLCLR function that takes a DateTime2 as input and returns another DateTime2. 我正在尝试编写一个SQLCLR函数,它将DateTime2作为输入并返回另一个DateTime2。 Based on this post I altered the parameter to be the C# type DateTime giving me the level of precision I require. 基于这篇文章,我将参数更改为C#类型DateTime,为我提供了所需的精度级别。 However because the input can be null I would like it to be DateTime?; 但是因为输入可以为null我希望它是DateTime? the return type as well. 返回类型也是如此。

using System;
using Microsoft.SqlServer.Server;

namespace SqlServer.Functions {
    public class UserDefinedFunctions {
        [SqlFunction(DataAccess = DataAccessKind.None)]
        public static DateTime? GetLocalTimeFromGMT(DateTime? dateTime) {
            if (dateTime.HasValue)
                return DateTime.SpecifyKind(dateTime.Value, DateTimeKind.Utc).ToLocalTime();
            else
                return (DateTime?)null;
        }
    }
}

The problem is I get the following error when I try to deploy: 问题是我尝试部署时遇到以下错误:

Error 1 Cannot find the type 'Nullable`1', because it does not exist or you do not have permission. 错误1找不到类型'Nullable`1',因为它不存在或您没有权限。 SqlServer.Functions SqlServer.Functions

I'm using Sql Server 2008 and Visual Studio 2008. 我正在使用Sql Server 2008和Visual Studio 2008。

Unfortunately Visual Studio 2008 does not support deployment of SQLCLR functions with many of the new features added in SQL Server 2008, including Nullable Types. 遗憾的是,Visual Studio 2008不支持部署SQLCLR功能,其中包括SQL Server 2008中添加的许多新功能,包括Nullable Types。 You can see the Connect item Bob Beauchemin filed on this issue, which has been fixed for Visual Studio 2010. 您可以在此问题上看到针对Visual Studio 2010修复的Connect项目 Bob Beauchemin。

There a workaround listed in the Connect item which indicates to Visual Studio that it can use the System.Core and System.Xml.Linq assemblies in SQLCLR, which should work for you. Connect项中列出了一种解决方法,它向Visual Studio指示它可以使用SQLCLR中的System.Core和System.Xml.Linq程序集,它们应该适合您。 Otherwise you can deploy the function to the database manually. 否则,您可以手动将该功能部署到数据库。

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

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