Set Current Date at default value in a data set?

雨燕双飞 提交于 2019-12-04 05:24:58

问题


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:


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" />


来源:https://stackoverflow.com/questions/11021832/set-current-date-at-default-value-in-a-data-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!