473,799 Members | 3,098 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.sqlConnect ion))
cmd.CommandType = CommandType.Sto redProcedure;
cmd.Parameters. Add("@F1", SqlDbType.NVarC har, 100);
cmd.Parameters["@F1"].value = "something" .
cmd.Parameters. Add("@F2", SqlDbType.DateT ime);
cmd.Parameters["@F2"].value = DateTime.Now.
cmd.ExecuteNonQ uery();

Regards,
John
Nov 17 '05 #1
4 7401
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.sqlConnect ion))
cmd.CommandType = CommandType.Sto redProcedure;
cmd.Parameters. Add("@F1", SqlDbType.NVarC har, 100);
cmd.Parameters["@F1"].value = "something" .
cmd.Parameters. Add("@F2", SqlDbType.DateT ime);
cmd.Parameters["@F2"].value = eventTime;
cmd.ExecuteNonQ uery();

"John J. Hughes II" <no@invalid.com > wrote in message
news:%2******** **********@TK2M SFTNGP14.phx.gb l...
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.sqlConnect ion))
cmd.CommandType = CommandType.Sto redProcedure;
cmd.Parameters. Add("@F1", SqlDbType.NVarC har, 100);
cmd.Parameters["@F1"].value = "something" .
cmd.Parameters. Add("@F2", SqlDbType.DateT ime);
cmd.Parameters["@F2"].value = DateTime.Now.
cmd.ExecuteNonQ uery();

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.sqlConnect ion))
cmd.CommandType = CommandType.Sto redProcedure;
cmd.Parameters. Add("@F1", SqlDbType.NVarC har, 100);
cmd.Parameters["@F1"].value = "something" .
cmd.Parameters. Add("@F2", SqlDbType.DateT ime);
cmd.Parameters["@F2"].value = DateTime.Now.
cmd.ExecuteNonQ uery();

Regards,
John

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

"Dan Bass" <Not Listed> wrote in message
news:u5******** ******@tk2msftn gp13.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.sqlConnect ion))
cmd.CommandType = CommandType.Sto redProcedure;
cmd.Parameters. Add("@F1", SqlDbType.NVarC har, 100);
cmd.Parameters["@F1"].value = "something" .
cmd.Parameters. Add("@F2", SqlDbType.DateT ime);
cmd.Parameters["@F2"].value = eventTime;
cmd.ExecuteNonQ uery();

"John J. Hughes II" <no@invalid.com > wrote in message
news:%2******** **********@TK2M SFTNGP14.phx.gb l...
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.sqlConnect ion))
cmd.CommandType = CommandType.Sto redProcedure;
cmd.Parameters. Add("@F1", SqlDbType.NVarC har, 100);
cmd.Parameters["@F1"].value = "something" .
cmd.Parameters. Add("@F2", SqlDbType.DateT ime);
cmd.Parameters["@F2"].value = DateTime.Now.
cmd.ExecuteNonQ uery();

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*********@di scussions.micro soft.com> wrote in message
news:3F******** *************** ***********@mic rosoft.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.sqlConnect ion))
cmd.CommandType = CommandType.Sto redProcedure;
cmd.Parameters. Add("@F1", SqlDbType.NVarC har, 100);
cmd.Parameters["@F1"].value = "something" .
cmd.Parameters. Add("@F2", SqlDbType.DateT ime);
cmd.Parameters["@F2"].value = DateTime.Now.
cmd.ExecuteNonQ uery();

Regards,
John

Nov 17 '05 #5

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

Similar topics

3
6175
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 -- @NumRecords <= 0 If I uncommment CLOSE and DEALLOCATE and check the syntax I get a
2
7293
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 behavior. Either I use .NET logic on the server and get server local time or I create xhtml pages directly from serialized objects. Eitherway I'll have to write my own logic to take care of utc offset. Is it possible to override XmlSerializer so...
6
27849
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 a date field. I have copied that section of the select statement below. You can see the old code that I used for Oracle--that works great--which I have commented out and the new line I added just below. When this sqr runs, it throws the...
4
9928
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
7256
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 only that I have to listen to them because they know it better. They told also that in a business situation it is better to use datetime.parseexact for changing cultures and not to use the globalization setting. I did not give them this sample,...
1
11255
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'." Explaining you the scene is the following Stored Proc.
4
5598
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 C# coding
2
11882
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). While I digged into the oslan database, I see the following: # Zone NAME GMTOFF RULES FORMAT Zone Asia/Calcutta 5:53:28 - LMT 1880 # Kolkata
2
5082
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, that is GMT +01:00 + daylight saving. And the web service handles datetimes correctly provided that they are also in CEST. But when some client calls my web service passing dates in UTC, the .Net runtime just ignores that fact and takes just the...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10251
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9072
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.