2

我想在我的新闻中保存时间和日期,但在下面的方法中我保存了用户的时间,如果他/她的时间错误,错误的时间将被保存。我怎样才能使用服务器的时间。

SqlConnection sqlconn = new SqlConnection(connStr);
SqlCommand sqlcmd = new SqlCommand("insert into SubmitManuscript(username,title,authors,type,date,upload,reviewer1,email1,reviewer2,email2,reviewer3,email3)values(@username,@title,@authors,@type,@date,@upload,@reviewer1,@email1,@reviewer2,@email2,@reviewer3,@email3)", sqlconn);
sqlcmd.Parameters.AddWithValue("@username", username);
sqlcmd.Parameters.AddWithValue("@title", Text_Title.Text);
sqlcmd.Parameters.AddWithValue("@authors", Text_Author.Text);
sqlcmd.Parameters.AddWithValue("@type", dd_Type.SelectedItem.Text);
sqlcmd.Parameters.AddWithValue("@date", DateTime.Now);
//sqlcmd.Parameters.AddWithValue("@upload", "~/Suppelment/" + 
4

1 回答 1

5

无需传递 datetime参数。只需在数据库中的表中将日期时间设置为默认值。这应该够了吧。

这是一个如何在表生成脚本中设置的链接:

如何设置日期时间列的默认值

编辑:您的表需要有一个特殊的 DateTimeStamp 列,该列将在您插入记录时自动设置系统日期时间。我正在放置一个简单的脚本,您可以将其用作示例。

注意:如果您已经有表格,那么只需从脚本开始ALTER TABLE

CREATE TABLE [dbo].[Employee]
(
    [EmployeeId] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,
    [BirthDate] [datetime] NOT NULL,
    [LastUpdateTime] [datetime] NOT NULL,

    CONSTRAINT [PK_Employee] 
        PRIMARY KEY CLUSTERED ([EmployeeId] ASC)
                    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                          IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                          ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Employee] 
    ADD CONSTRAINT [DF_Employee_LastUpdateTime]  
        DEFAULT (getdate()) FOR [LastUpdateTime]
GO

--- You may also use multiple record insert statement in SQL Server 2008
--- however, I will use a traditional approach here
INSERT INTO [dbo].[Employee] (Name, BirthDate)
VALUES ('Smith', '04/14/1979')

INSERT INTO [dbo].[Employee] (Name, BirthDate)
VALUES ('Jhone', '11/08/1983')
GO
于 2012-06-30T06:58:55.920 回答