views:

484

answers:

5

I have a SqlCommand which inserts a datetime into column using a SqlParameter. At present I use DateTime.Now as the value of this parameter.

I believe this will add the datetime on the user's PC which is not consistent.

How do I change this so the datetime of the database server is inserted?

EDIT:

I should have made it clear this was just for inserts

A: 

Hi, http://doc.ddart.net/mssql/sql70/ga-gz%5F1.htm Best Regards, Iordan

IordanTanev
This is for SQL 7... and it's still just a copy of Books On Line that was never updated
gbn
it does not matter GETDATE() work with every MSSQL server
IordanTanev
+6  A: 

Don't pass in a parameter for the current date; get SQL Server to generate it.

The SQL to do what you described is GETDATE(), though you may want to consider using GETUTCDATE(), as it will behave better with respect to timezone changes (and moving the server!)

See MSDN for more details.

Dave Roberts
Can a SQL Server function be specified as the value of a SQLParameter? If not I assume the best method would be to use a default constraint as suggested in another answer
Gary Joynes
I don't think it can. Default constraint would work well if you're currently inserting directly into the table - I was assuming that this was part of a larger Stored Procedure.
Dave Roberts
+1: IMHO, this *is* the best solution. Nonetheless, if DataSets are used, the DateColumn.DateTimeMode may be used to have all users have consistent TimeZone settings (Utc, I assume). See: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.datetimemode.aspx
van
@Dave: you may add the DEFAULT CONSTRAINT as marc_s suggested, or you can just call GETDATE() in your SP. In both cases you would not pass the value as the parameter to the SP.
van
@van: Indeed - a Default Constraint will work in either circumstance. If I already had a sproc I'd probably keep the logic in the sproc and out of the table definition, but each to their own.
Dave Roberts
Thanks for all the comments. As I am using a paramterised SQL string rather than a stored proc, and this requirement is just for inserts I have accept the default constraint answer
Gary Joynes
A: 

In that case, don't pass a datetime value as a parameter. Instead use the GetDate() function and insert that result in the table

insert into X (Id, Col1, Col2) values (1, 'Customer1', GetDate())

Hope this helps

tzup
+4  A: 

Is this strictly for INSERTs ? Or for updates as well?

If it's for INSERTs only, you could declare a DEFAULT constraint on your DATETIME column on the SQL Server side, and then just not insert a value from the client side - thus using the server's default.

In SQL:

 ALTER TABLE YourTable 
   ADD CONSTRAINT DF_YourTable_YourColumn
     DEFAULT (GETDATE()) FOR YourColumn

and then in your SQL command, just don't mention the "YourColumn" and don't provide a value for it - the server will use getdate() to get the current date/time on the server instead.

Marc

marc_s
A: 

You may use stored-procedure and GETDATE(),

tableName(no: int, name: varchar(40), date datetime)

Stored procedure

CREATE PROCEDURE  AddRec
@no int,
@name varchar(40)
AS
insert into tableName
 values (@no,@name,getdate())
RETURN

Code to execute stored-procedure

SqlConnection cn=new SqlConnection("Your_Cn_Str");
SqlCommand cmd=new SqlCommand();
cmd.CommandText="AddRec";
cmd.CommandType=CommandType.StoredProcedue;
cmd.Connection=cn;

cmd.Parameters.AddWithValue("@no",10);
cmd.Parameters.AddWithValue("@name","Foo");

cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
adatapost