468,119 Members | 1,976 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,119 developers. It's quick & easy.

Extracting date/time from database

Sorry for posting .. just half a message but i pressed enter outside the input field :here is entire problem . tnx in advance

i'm trying to extract a datetime value from a mysql database.The feld is in format
yyyy-mm-dd 00:00:00 and the datareader extracts something like this '4/1/2007' ,i don't figure it out where i go wrong

i tried with allow zero datetime=true, but is not working , i tried all the possibilities .. i don't know where is the problem ..
i tried with DR.getDateTime(i)

my code is :
conn3.ConnectionString = myConnString
conn3.Open()
comanda.Connection = conn3
Dim sqlQueryCommand As New MySqlCommand("select * from " & table & " ;", conn3)
Dim DR As MySqlDataReader
DR= sqlQueryCommand.ExecuteReader()
While DR.Read
For i = 0 To DR.FieldCount - 1
Dim tip = DR.GetFieldType(i)
If tip.ToString = "System.DateTime" Then
Dim exacttime = DR.GetDateTime(i)
Console.write(exacttime)
End If
Next
End while
conn3.close()
Apr 16 '07 #1
5 1625
do u want the format yyyy/MM/dd
try this code..

exacttime.tostring("yyyy/MM/dd")

Expand|Select|Wrap|Line Numbers
  1. conn3.ConnectionString = myConnString
  2. conn3.Open()
  3. comanda.Connection = conn3
  4. Dim sqlQueryCommand As New MySqlCommand("select * from " & table & " ;", conn3)
  5. Dim DR As MySqlDataReader
  6. DR= sqlQueryCommand.ExecuteReader()
  7. While DR.Read
  8. For i = 0 To DR.FieldCount - 1
  9. Dim tip = DR.GetFieldType(i)
  10. If tip.ToString = "System.DateTime" Then
  11. Dim exacttime = DR.GetDateTime(i)
  12. Console.write(exacttime.tostring("yyyy/MM/dd")
  13. End If
  14. Next
  15. End while
  16. conn3.close()
  17.  
  18.  
  19.  
Apr 16 '07 #2
Hello again ..
I succeed in manage the datetime data receive from datareader..
i used a old method .. or not so optimal ..
If tip.ToString = "System.DateTime" Or tip.ToString = "System.TimeSpan" Then
Dim dataextrasa As MySql.Data.Types.MySqlDateTime = arata.GetDateTime(i).ToString
Dim ora As String = Nothing
Dim minut As String = Nothing
Dim secunda As String = Nothing
Dim luna As String = Nothing
Dim ziua As String = Nothing
If dataextrasa.Hour.ToString = "0" Or dataextrasa.Hour.ToString = Nothing Then
ora = "00"
Else
ora = dataextrasa.Hour.ToString
End If
If dataextrasa.Minute.ToString = "0" Or dataextrasa.Minute.ToString = Nothing Then
minut = "00"
Else
minut = dataextrasa.Minute.ToString
End If
If dataextrasa.Second.ToString = "0" Or dataextrasa.Second.ToString = Nothing Then
secunda = "00"
Else
secunda = dataextrasa.Second.ToString
End If
'aici tre sa pun un 0 la luna si la zi .. in caz de sunt mai mici k 10
If dataextrasa.Month.ToString.Length = 1 Then
luna = "0" & dataextrasa.Month.ToString
Else
luna = dataextrasa.Month.ToString
End If

If dataextrasa.Day.ToString.Length = 1 Then
ziua = "0" & dataextrasa.Day.ToString
Else
ziua = dataextrasa.Day.ToString
End If
Dim combin As String = dataextrasa.Year.ToString & "-" & luna & "-" & ziua & " " & ora & ":" & minut & ":" & secunda
Console.Write("'" & combin & "'")
End If


-i think is a good method .. is not so optimal .. i hope helps
Apr 16 '07 #3
Plater
7,872 Expert 4TB
I am not sure why you keep checking to see if the returned value is of type DateTime, I've never had to do any checks like that, ToString always worked just fine.

SQL (well at least MSSQL, pretty sure other implimentations have it as well) has a Convert function, I use it all the time and it doesn't involve much of anything.
http://msdn2.microsoft.com/en-us/library/ms187928.aspx

Convert it to the form you want and tell it to return like type varchar(20) or something. You'll get a type string in your code but it will be formated how you like it.
Apr 16 '07 #4
r035198x
13,262 8TB
Changed thread title.
Apr 16 '07 #5
tnx guys .. right now i make the final "makeup" before i upload the files on the sourceforge.net .. i hope is not a problem .. if i wrote this internet address here ..

is about a mysqldump like utility wrote in vb.net .using the mysql connector .. i hope will be usefull ..
Apr 16 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Alfred | last post: by
2 posts views Thread by bill tie | last post: by
1 post views Thread by jerryyang_la1 | last post: by
4 posts views Thread by dexter48 | last post: by
13 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.