开发者

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

I've sql Server 2000 DB which includes Stored Procedure that return Current DateTime. And I've the procedure that call this procedure:

 [Function(Name = "dbo.spGetDBDateTime")]
public ISingleResult<DateTime?> GetDBDateTime()
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())),new o开发者_如何学编程bject[]{} );
    return ((ISingleResult<DateTime?>)(result.ReturnValue));
}

I've got such error: The type 'System.Nullable`1[System.DateTime]' must declare a default (parameterless) constructor in order to be constructed during mapping.

Could you help me, please?


You don't really need a custom stored procedure to get the current server datetime since the server already offers the T-SQL function GETDATE(). All you need to do is create a LINQ to SQL method pointing to that function. Add the new method to your DataContext partial class (right-click on the designer surface and go View Code) like this:

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;
        }
    }
}

And call it like this:

DateTime serverDate = context.GetSystemDate();

Partial credit for this answer goes here :-)

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


Sorry about my English ))) Hi, everyone! Thank's for your helps, but I have just solved this problem self.

In MS Visual Studio 2008 or later versions used Server Explorer => Data Connections =>Add new Connection and connected to my DB. Then in server Explorer was showed, as well as other items of DB, Stored Procedures and I just drew dbo.spGetDBDateTime procedure in code and VS generated wrap for this procedure, so I could called it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜