tags:

views:

43

answers:

3

Hello. At my domains for audit purposes I've got UpdatedOn property. Now I set it at client before updating to DateTime.Now. But a lot of users have incorrect datetime at theirs machines, so i need use server datetime (getdate(),now()...) How can i implement this strategy?

A: 

I'm going to suggest this http://mastercsharp.com/article.aspx?ArticleID=66&TopicID=7 for you since this might be what you're looking for. Or you could just create a webservice to return the server time...

BuildStarted
It's very hard for this easy think. I want use my sql server to return its datetime
Andrew Kalashnikov
yeah, i thought about that after i posted this :)
BuildStarted
+1  A: 

You can make this property a read-only/query only property and so ignore application code entirely. Just set a default value on insert of a row

CREATE TABLE [dbo].[FooTable](
.
.
.
[UpdatedOn] [datetime] NOT NULL CONSTRAINT [DF_FooTable_UpdatedOn] DEFAULT (Getdate()),
.
.

this will ensure that when a value is not passed then the column will take the value of Getdate()

Jaguar
+1  A: 

In our project, we customized our MsSql2005Dialect.

    public class MsSql2005Dialect : NHibernate.Dialect.MsSql2005Dialect
{
    public MsSql2005Dialect()
    {
        RegisterFunction("getDate",
            new NoArgSQLFunction("convert(datetime, floor(convert(float, getdate())))", NHibernateUtil.DateTime, false));
        RegisterFunction("getDateTime",
            new NoArgSQLFunction("getdate", NHibernateUtil.DateTime, true));
    }
}

and we register this custom dialect in nhibernatehelper: Dialect dialect = Dialect.GetDialect(configuration.Properties);

register in the hibernate.cfg.xml: SAGP.DataAccess.Dialects.MsSql2005Dialect, SAGP.DataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

and use this in hql like: "select getDateTime() from RandomTableFromYourSystem";

jaspion