Format SqlDataReader as date
Monday 01 June 2009
If you would like to format an SqlDataReader in a nice date format, below is an example showing how to do so:
-
Dim objDR As SqlDataReader
-
-
DateTime.Parse(objDR("date")).ToString("dddd dd MMMM yyyy")
The above displays the date like this 'Monday 01 June 2009', you can obviously change this by changing the code within the 'ToString' brackets.
An example of this in full use is below:
- Dim MyConnection As SqlConnection
- MyConnection = New SqlConnection()
- MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
-
- Dim sql As String = "SELECT * FROM TABLE WHERE TableID = 1"
- Dim objDR As SqlDataReader
- Dim CmdR As New SqlCommand(sql, MyConnection)
-
- MyConnection.Open()
- objDR = CmdR.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
- While objDR.Read()
- Response.Write(DateTime.Parse(objDR("date")).ToString("dddd dd MMMM yyyy"))
- End While
- MyConnection.Close()
|
(0)
|
|
|