繁体   English   中英

ExecuteMethodCall:类型'System.Nullable`1 [System.DateTime]'必须声明一个默认(无参数)构造函数

[英]ExecuteMethodCall: The type 'System.Nullable`1[System.DateTime]' must declare a default (parameterless) constructor

我有sql Server 2000 DB,其中包括返回当前DateTime的存储过程。 我有调用此过程的过程:

 [Function(Name = "dbo.spGetDBDateTime")]
public ISingleResult<DateTime?> GetDBDateTime()
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())),new object[]{} );
    return ((ISingleResult<DateTime?>)(result.ReturnValue));
}

我遇到了这样的错误:类型'System.Nullable`1 [System.DateTime]'必须声明一个默认(无参数)构造函数,以便在映射期间进行构造。

请问你能帮帮我吗?

因为服务器已经提供了T-SQL函数GETDATE(),所以您实际上不需要自定义存储过程来获取当前服务器的日期时间。 您需要做的就是创建一个指向该函数的LINQ to SQL方法。 将新方法添加到您的DataContext子类中(在设计器图面上单击鼠标右键并转到“查看代码”),如下所示:

using System.Data.Linq.Mapping;
using System.Reflection;
using System;

namespace L2STest
{
    partial class MyDataContext
    {
        [Function(Name = "GetDate", IsComposable = true)]
        public DateTime GetSystemDate()
        {
            MethodInfo mi = MethodBase.GetCurrentMethod() as MethodInfo;
            return (DateTime)this.ExecuteMethodCall(this, mi, new object[] { }).ReturnValue;
        }
    }
}

并这样称呼它:

DateTime serverDate = context.GetSystemDate();

这个答案的部分功劳在这里:-)

http://peteohanlon.wordpress.com/2008/01/11/sql-server-getdate-and-linq-to-sql/

对不起,我的英语)))大家好! 谢谢您的帮助,但我刚刚解决了这个问题。

在MS Visual Studio 2008或更高版本中,使用服务器资源管理器=>数据连接=>添加新连接并连接到我的数据库。 然后在服务器资源管理器中显示了DB以及存储过程的其他项目,我只是在代码中绘制了dbo.spGetDBDateTime过程,并且VS为该过程生成了包装,所以我可以称之为它。

暂无
暂无

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

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