问题
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