Set Current Date at default value in a data set?

2019-07-28 11:13发布

I have a dataset that i am trying to set the default value as the current date and time. I believe that value needs to be a literal. Is there a certain way to do this? Because I am not able to do this using System.DateTime it seems.Any advice would be a great deal of help.

1条回答
家丑人穷心不美
2楼-- · 2019-07-28 11:23

You can set the default value of relevant column as

dataTable1.Columns["dateTimeColumn"].DefaultValue = System.DateTime.Now;

If you fetch data from a SQL Server database, then you can set the default value using the GETDATE function:

ALTER TABLE MyTable
ADD CONSTRAINT DF_MyTable_MyColumn
DEFAULT GETDATE() FOR MyColumn

Using this code:

UploadHistory.Columns["FileUploadDate"].DefaultValue = System.DateTime.Now;

When the table is written out as XML:

<xs:element name="FileUploadDate" 
            type="xs:dateTime" 
            default="2017-03-13T11:39:26.7980069-05:00" 
            minOccurs="0" />
查看更多
登录 后发表回答