473,545 Members | 1,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about DateTime

I have a DataRow dr retrieved from a database. One of the column is datetime
type. How do I convert it to DateTime structure in .NET?
DateTime dt = DateTime.Parse( dr[datetimecolumn].ToString()) will drop the
millisecond part. I want to keep the millisecond part.
Jul 28 '08 #1
4 1281
You can probably just cast; try looking at
dr[datetimecolumn].GetType() to see what it actually is...

I suspect this will work:

DateTime dt = (DateTime)dr[datetimecolumn];

Marc
Jul 28 '08 #2
Roy wrote on 28-7-2008 :
I have a DataRow dr retrieved from a database. One of the column is datetime
type. How do I convert it to DateTime structure in .NET?
DateTime dt = DateTime.Parse( dr[datetimecolumn].ToString()) will drop the
millisecond part. I want to keep the millisecond part.
Try casting it directly to DateTime:
DateTime dt = (DateTime)dr[datetimecolumn];

but watch out for DbNull values, as that will cause a crash (you can't
cast a DbNull to DateTime).

Hans Kesting
Jul 28 '08 #3
What if I want to dynamically construct a TSQL and use it in the WHERE clause?
"WHERE datetimecolumn = " + dt.ToString() will not include the millisecond
either.
Thanks.

"Hans Kesting" wrote:
Roy wrote on 28-7-2008 :
I have a DataRow dr retrieved from a database. One of the column is datetime
type. How do I convert it to DateTime structure in .NET?
DateTime dt = DateTime.Parse( dr[datetimecolumn].ToString()) will drop the
millisecond part. I want to keep the millisecond part.

Try casting it directly to DateTime:
DateTime dt = (DateTime)dr[datetimecolumn];

but watch out for DbNull values, as that will cause a crash (you can't
cast a DbNull to DateTime).

Hans Kesting
Jul 28 '08 #4
What if I want to dynamically construct a TSQL and use it in the WHERE clause?
"WHERE datetimecolumn = " + dt.ToString() will not include the millisecond
either.
Thanks.
A good rule of thumb: never concatenate user input; use parameters
instead. This saves you from internationalis ation (i18n) issues (does
130,000 mean "130 thousand", or "130 point zero"?), and from injection
attacks (http://xkcd.com/327/).

So your TSQL would be "WHERE datetimecolumn = @somearg", and you'd add
a parameter called "somearg" to the command's parameters collection.

Marc
Jul 28 '08 #5

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

Similar topics

4
1472
by: Jan Danielsson | last post by:
Hello all, I have a list of servers which an application connects to. If the connection fails, the application should mark the server as temporarily unavailable, and should try to use the server again after x units of time. In C, I would do this: server.invalidUntil = time(NULL) + 5*60; // five minute delay
0
2199
by: Ryan R. Tharp | last post by:
------=_NextPart_000_03D1_01C3456A.1BA390A0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable This question came up at work today, would like to get some fellow = developer's opinions: Scenario 1: 2 tables to track checks
6
1805
by: Carl | last post by:
Hi, to be use with SQL server database 1- From window form textbox object From this assignement, txtTOTAL.Text="0.00m"; How do you convert back to decimal type ? How do you assign a default datetime value to it ? How do you convert back the text enter by user into datetime ?
8
1519
by: Jimmie | last post by:
Hiya all, very quick question, I hope .. I have an xml dump of some old data that I need to parse and input into a new system. A couple of date fields are in there that have the following data (just a sample) .. .. AvailableTimeStamp:1073866182 VerifiedTimeStamp:1073839057 AvailableTimeStamp:1074352648 VerifiedTimeStamp:1074401625
5
1424
by: Dinçer | last post by:
I need to compare dates. But this way: I get a date from database. I need to understand if this date is newer then "6 months" or not. What I need is get the current date, calculate the date 6 months ago, compare "the date from DB" & "the date 6 months ago from now" show the result.
18
2033
by: Nick Z. | last post by:
I am writing a reusable class for logging. My goal is to make it as fast and as robust as possible, while keeping memory usage to the lowest. All members are static. For some reason I'm stuck on the following design question. Obviously you need a stream to write the log file. Should this stream be created every time the log needs to be...
3
1523
by: Ken H | last post by:
Hi I have a question about architecting solutions.. I have a part of a project which requires me to track person details (name, addresses, etc... Should I be creating Person objects, Address objects etc as Business layer classes and then have an data access layer which actually save / updates / delete to and from the DB.. If so, and bearing...
4
1271
by: Philippe Martin | last post by:
Hi, I need to get the date and time under Windows and Linux but need the information visible to the user (cannot find my words) not the sytem information (ex: a PC setup on greenwich but the date/time displayed are relative to some other place. Regards, Philippe
7
1989
by: JamesG | last post by:
Hi, I need to convert the current time to the format "hh:mm:ss" and date to the format "yyyy-MM-dd". The result of both conversions needs to be of System.DateTime format. I've tried using DateTime.ParseExact and DateTimeFormatInfo but failed, most examples I see convert the final result to a string... I dont want that!
3
1670
by: Microsoft | last post by:
Hi I have a c# program that continually runs 24/7 and performs a variety of tasks based on a timer. There is one routine that hangs every Saturday morning without fail. If I restart the program throughout the week is still hangs. I can restart it immediatly after the hang and it runs fine until next Saturday. There is nothing different...
0
7398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
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...
0
7752
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5325
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...
0
4944
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...
0
3449
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...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.