将当前日期设置为数据集中的默认值?
问题描述:
我有一个数据集,我试图将默认值设置为当前日期和时间。我认为价值需要是一个字面意思。有没有办法做到这一点?因为我不能使用System.DateTime这样做它似乎。任何建议将是一个很大的帮助。
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.
答
您可以将相关列的默认值设置为
You can set the default value of relevant column as
dataTable1.Columns["dateTimeColumn"].DefaultValue = System.DateTime.Now;
如果从SQL Server数据库中获取数据,则可以使用 GETDATE
函数:
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
使用以下代码:
UploadHistory.Columns["FileUploadDate"].DefaultValue = System.DateTime.Now;
当表格写成 XML
:
<xs:element name="FileUploadDate"
type="xs:dateTime"
default="2017-03-13T11:39:26.7980069-05:00"
minOccurs="0" />