473,411 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Incorrect DateTime.Now when saved to SQL

I have several functions that save the time when they did certain tasks to
an SQL table. This was working fine as far as I could tell but I now have
systems that are updating thousands of records and a very small percentage
are showing as future dates by seconds to hours. As a guess I would say
about 90% are correct

Does any body see what I am doing wrong?

Below is the basic code:

In the SQL I have the following stored procedure:

Create Procedure InsertData
(
@F1 nvarchar(100),
@F2 datetime
)
AS
SET NOCOUNT OFF;
UPDATE TBL1 SET [F2]=@F2 WHERE [F1]=@F1

C# Code

SqlCommand cmd = ("[InsertData]", this.sqlConnection))
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@F1", SqlDbType.NVarChar, 100);
cmd.Parameters["@F1"].value = "something".
cmd.Parameters.Add("@F2", SqlDbType.DateTime);
cmd.Parameters["@F2"].value = DateTime.Now.
cmd.ExecuteNonQuery();

Regards,
John
Nov 17 '05 #1
4 7382
how long does it take to up date thousands of records?

would you expect the date to be the same for each one? Because the code
below will take the date at the time just before each command was able to
execute.

If you wanted to update the records with some date of some event, then you
should save this into a DateTime object, then reference this, rather than
referencing DateTime.Now every time.

eg.
DateTime eventTime = DateTime.Now;

// loads of other code perhaps

SqlCommand cmd = ("[InsertData]", this.sqlConnection))
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@F1", SqlDbType.NVarChar, 100);
cmd.Parameters["@F1"].value = "something".
cmd.Parameters.Add("@F2", SqlDbType.DateTime);
cmd.Parameters["@F2"].value = eventTime;
cmd.ExecuteNonQuery();

"John J. Hughes II" <no@invalid.com> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
I have several functions that save the time when they did certain tasks to
an SQL table. This was working fine as far as I could tell but I now have
systems that are updating thousands of records and a very small percentage
are showing as future dates by seconds to hours. As a guess I would say
about 90% are correct

Does any body see what I am doing wrong?

Below is the basic code:

In the SQL I have the following stored procedure:

Create Procedure InsertData
(
@F1 nvarchar(100),
@F2 datetime
)
AS
SET NOCOUNT OFF;
UPDATE TBL1 SET [F2]=@F2 WHERE [F1]=@F1

C# Code

SqlCommand cmd = ("[InsertData]", this.sqlConnection))
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@F1", SqlDbType.NVarChar, 100);
cmd.Parameters["@F1"].value = "something".
cmd.Parameters.Add("@F2", SqlDbType.DateTime);
cmd.Parameters["@F2"].value = DateTime.Now.
cmd.ExecuteNonQuery();

Regards,
John

Nov 17 '05 #2
How many machines run the update sp ?
When using System.DateTime.Now you are depending on the clocks of the
individual PCs running the .NET code.

For conformity you may want to use the SQL Server clock by using GETDATE()

Best regards,

Nico De Greef

"John J. Hughes II" wrote:
I have several functions that save the time when they did certain tasks to
an SQL table. This was working fine as far as I could tell but I now have
systems that are updating thousands of records and a very small percentage
are showing as future dates by seconds to hours. As a guess I would say
about 90% are correct

Does any body see what I am doing wrong?

Below is the basic code:

In the SQL I have the following stored procedure:

Create Procedure InsertData
(
@F1 nvarchar(100),
@F2 datetime
)
AS
SET NOCOUNT OFF;
UPDATE TBL1 SET [F2]=@F2 WHERE [F1]=@F1

C# Code

SqlCommand cmd = ("[InsertData]", this.sqlConnection))
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@F1", SqlDbType.NVarChar, 100);
cmd.Parameters["@F1"].value = "something".
cmd.Parameters.Add("@F2", SqlDbType.DateTime);
cmd.Parameters["@F2"].value = DateTime.Now.
cmd.ExecuteNonQuery();

Regards,
John

Nov 17 '05 #3
Thanks for your response, see in line
John

"Dan Bass" <Not Listed> wrote in message
news:u5**************@tk2msftngp13.phx.gbl...
how long does it take to up date thousands of records?
I am only changing the date to one record at a time, the thousands are being
updated during the course of the day so 24 hours. Two records could be
updated a few millisecond apart and a few minutes. The might also be update
from different threads.
would you expect the date to be the same for each one? Because the code
below will take the date at the time just before each command was able to
execute.
No I expect the date and time to be different. I am only trying to tell the
system when the server changed the record.
If you wanted to update the records with some date of some event, then you
should save this into a DateTime object, then reference this, rather than
referencing DateTime.Now every time.
In places where I save the same date to multiple records I do that and seems
to have the same problem. In this case the time span from when I save the
date to an object to when I used it would be too long.
eg.
DateTime eventTime = DateTime.Now;

// loads of other code perhaps

SqlCommand cmd = ("[InsertData]", this.sqlConnection))
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@F1", SqlDbType.NVarChar, 100);
cmd.Parameters["@F1"].value = "something".
cmd.Parameters.Add("@F2", SqlDbType.DateTime);
cmd.Parameters["@F2"].value = eventTime;
cmd.ExecuteNonQuery();

"John J. Hughes II" <no@invalid.com> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
I have several functions that save the time when they did certain tasks to
an SQL table. This was working fine as far as I could tell but I now have
systems that are updating thousands of records and a very small percentage
are showing as future dates by seconds to hours. As a guess I would say
about 90% are correct

Does any body see what I am doing wrong?

Below is the basic code:

In the SQL I have the following stored procedure:

Create Procedure InsertData
(
@F1 nvarchar(100),
@F2 datetime
)
AS
SET NOCOUNT OFF;
UPDATE TBL1 SET [F2]=@F2 WHERE [F1]=@F1

C# Code

SqlCommand cmd = ("[InsertData]", this.sqlConnection))
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@F1", SqlDbType.NVarChar, 100);
cmd.Parameters["@F1"].value = "something".
cmd.Parameters.Add("@F2", SqlDbType.DateTime);
cmd.Parameters["@F2"].value = DateTime.Now.
cmd.ExecuteNonQuery();

Regards,
John


Nov 17 '05 #4
Thanks for the response,

The reason I am doing it the way I am doing it is I want the date of the
local machine. Also in some places I am writing the same date to a group of
records and yes as Dan pointed out I save it to an object first.

In the case of the problem being reported to me by my customer the SQL
Server and my application are running on the same computer so I would expect
the dates to be the same or at least very close. The date differencesI am
seeing is up to an hour different.

Regards,
John

"Nico De Greef" <Ni*********@discussions.microsoft.com> wrote in message
news:3F**********************************@microsof t.com...
How many machines run the update sp ?
When using System.DateTime.Now you are depending on the clocks of the
individual PCs running the .NET code.

For conformity you may want to use the SQL Server clock by using GETDATE()

Best regards,

Nico De Greef

"John J. Hughes II" wrote:
I have several functions that save the time when they did certain tasks
to
an SQL table. This was working fine as far as I could tell but I now
have
systems that are updating thousands of records and a very small
percentage
are showing as future dates by seconds to hours. As a guess I would say
about 90% are correct

Does any body see what I am doing wrong?

Below is the basic code:

In the SQL I have the following stored procedure:

Create Procedure InsertData
(
@F1 nvarchar(100),
@F2 datetime
)
AS
SET NOCOUNT OFF;
UPDATE TBL1 SET [F2]=@F2 WHERE [F1]=@F1

C# Code

SqlCommand cmd = ("[InsertData]", this.sqlConnection))
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@F1", SqlDbType.NVarChar, 100);
cmd.Parameters["@F1"].value = "something".
cmd.Parameters.Add("@F2", SqlDbType.DateTime);
cmd.Parameters["@F2"].value = DateTime.Now.
cmd.ExecuteNonQuery();

Regards,
John

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: teddysnips | last post by:
In the script below is the DDL to create some tables and a UDF. What I'm interested in is the UDF at the end. Specifically, these few lines: --CLOSE OTRate --DEALLOCATE OTRate ELSE --...
2
by: BjörnHolmberg | last post by:
Hi everyone! In the following code we get a UTC offset in xml. Since I want my WAP users in UTC+02:00 to see data on a server in some US time zone, a lot of confusion will be created from this...
6
by: Brenda | last post by:
We are currently switching to DB2/AIX. I am modifying my sqrs to work on this new platform. (We are currently on Oracle). I am having a problem with an sqr that has a reference to a variable for...
4
by: Carl | last post by:
Can you tell me what is wrong with this syntax ? string select = "UPDATE .. " + "(,,,,,,, ,,,, ,,, , , , ) " + " VALUES (@id,@clientid,@total,@tps,@tvq,@gtotal,@datefac,@datepay,
11
by: Cor Ligthert | last post by:
Hello everybody, Jay and Herfried are telling me every time when I use CDate that using the datetime.parseexact is always the best way to do String to datetime conversions. They don't tell why...
1
by: Sandesh | last post by:
Hello All, Me saying " has any body come across such error would be underestimating". Well I am getting a very peculiar and unique error "Line 1: Incorrect syntax near 'Actions'." ...
4
by: Manikandan | last post by:
Hi, I'm inserting a datetime values into sql server 2000 from c# SQL server table details Table name:date_test columnname datatype No int date_t DateTime ...
2
by: Sanjay | last post by:
Hi All, Using pytz, I am facing a problem with Asia/Calcutta, described below. Asia/Calcutta is actually IST, which is GMT + 5:30. But while using pytz, it was recognized as HMT (GMT + 5:53)....
2
by: nightwatch77 | last post by:
Hi, does anyone know why .Net incorrectly handles time zone conversion when passing DateTime through web services? The problem is that it seems to ignore the time zone part. My time zone is CEST,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.