User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 455,967 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,741 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 1818 | Replies: 5
Reply
Join Date: Nov 2007
Posts: 3
Reputation: LMR is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
LMR LMR is offline Offline
Newbie Poster

Help retrieve time from database

  #1  
Nov 24th, 2007
I have stored time in an access database with a format of MediumTime, which displays in access as I want it, such as 7:30 AM. However, when I retrieve the time in my c# program, it also has a date (which is the wrong date anyway since I didn't enter one in access).

queryString = " SELECT Time FROM database";

I can change the time to a string and parse just the time, but then when I sort the time (as a string) it doesn't sort in the correct order. I've also tried using DateTime.ParseExact, but can't seem to get that to work.

Thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Posts: 3
Reputation: LMR is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
LMR LMR is offline Offline
Newbie Poster

Re: retrieve time from database

  #2  
Nov 24th, 2007
another approach maybe? I am putting this data into a datagrid. Is there a way to change the column format to just display the time and not the date?
Reply With Quote  
Join Date: Nov 2006
Location: Bonners Ferry, ID
Posts: 280
Reputation: JerryShaw is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 39
JerryShaw JerryShaw is offline Offline
Posting Whiz in Training

Re: retrieve time from database

  #3  
Nov 25th, 2007
Don't know if this will help you but you can get the Time of a DateTime using the TimeSpan

            DateTime data = new DateTime(2007, 11, 10, 13, 15, 0);
            TimeSpan time = data.TimeOfDay;
            //  time.ToString() returns "13:15:00"
Reply With Quote  
Join Date: Nov 2007
Posts: 3
Reputation: LMR is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
LMR LMR is offline Offline
Newbie Poster

Re: retrieve time from database

  #4  
Nov 27th, 2007
Jerry,

Thanks. That code works great to get a string, but not quite what I was trying to do.

I have the users enter a time (no date required) that is stored in an Access database. When I retrieve it (using SQL) the format is DateTime and looks something like ...

{08/10/1899 1:15:00 PM}

The time is good, but the date is a nonsense date. I wrote the following

(dataTable is the table I retrieved from the database containing the time)

char[] splitter = { ' ' };
string[] arInfo = new string[4];

tempDate = dataTable.Rows[i][0].ToString(); //0th column is datetime

arInfo = tempDate.Split(splitter);
arInfo[1] += " ";
newDate = String.Concat(arInfo[1], arInfo[2]);

This gets me a string with just the time ... although your way is much more straightforward. The problem I have is that I've bound the dataTable to a dataGrid, so the string doesn't help alot. I guess I need to replace the dataTable with the string instead of a DateTime.

The other thing I am pursueing is something in the SQL command to parse or cast the values such as

SELECT TIMEOFDAY(Appointments.Time) FROM ....

but that hasn't seemed to work either.

Thanks again for your response.
Reply With Quote  
Join Date: Nov 2007
Location: California
Posts: 18
Reputation: shaulf is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
shaulf's Avatar
shaulf shaulf is offline Offline
Newbie Poster

Re: retrieve time from database

  #5  
Nov 29th, 2007
of course use: {0:t}
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 102
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: retrieve time from database

  #6  
Dec 6th, 2007
Unfortunately, no matter what you can do you can't get just a time value from the database. Shaulf's suggestion will work, but that's for a string.

Here's a suggestion, though. If you're working with Time, there may end up being a time where you deal with something that happens at 11:59PM and ends at 12:30AM the next day. If you just store the time values, how will you deal with this? Just a thought. Then it starts to make sense why there's a DateTime value instead of just a Time value...

But also, I think you can cast a SqlDataType that is a Date/Time value to a DateTime data type. From there, the closest you might be able to get is use the .ToShortTime() method or something just to get the time. But then again, it only returns it in a string, too...

The DateTime object has all kinds of properties, like Hours, Minutes, Seconds, etc, that you can use in some form or another if you're wanting to do things like Time adding or similar. I suggest you look into those...
Alex Cavnar, aka alc6379
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 9:07 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC