Click here to Skip to main content
Click here to Skip to main content

Get Database Server Date and Time

By , 11 Oct 2007
 

Introduction

This Small application shows how to display the Server's date and time in your application.

Screenshot - GetServerDateAndTime.jpg


Background

The requirement is to display the database servers Current System Date and Time in the client application. When the client application starts it fetches the Current Date and Time on the Remote server(Database Server) and keeps incrementing it by one second using a Timer.

Using the code

Code for the ServerDateTime Class:

Imports System.Data

Imports System.Data.SqlClient

Public Class ServerDateTime

Public dtServerDateTime As DateTime

Private Shared tmrLocalTimer As New System.Windows.Forms.Timer()

' Flag to check whether it is First time run. 

Dim IsFirstTime As Boolean = True

Public Sub TimerEventProcessor(ByVal myObject As Object, ByVal myEventArgs As EventArgs)

'Check whether it is First time run. 

' If yes then Get Server time and display it in the Label.

If IsFirstTime = True Then

Dim cmd As SqlCommand

Dim tmpConnection As SqlConnection

Dim strConnectionString As String

Try

' Read ConnectionString from Application Configuration file.

strConnectionString = System.Configuration.ConfigurationManager.AppSettings("ServerConnectionString")

tmpConnection = New SqlConnection(strConnectionString)

' Open the Connection

tmpConnection.Open()

' Initialize the Command object with commandText and SQLConnection

cmd = New SqlCommand("Select GetDate()", tmpConnection)

'Execute the query and return the Server Date Time

dtServerDateTime = cmd.ExecuteScalar

' Display the Server Date Time in the Label.

GetServerDateAndTime.lServerDateTime.Text = Format(dtServerDateTime, "dd/MM/yyyy hh:mm:ss tt")

' Set the Flag to False

IsFirstTime = False

Catch ex As Exception

MsgBox(ex.Message)

Finally

If tmpConnection.State = ConnectionState.Open Then

cmd = Nothing

tmpConnection.Close()

End If

End Try

Else

'Add one Second to the dtServerDateTime

dtServerDateTime = DateAdd(DateInterval.Second, 1, dtServerDateTime)

End If

tmrLocalTimer.Enabled = True

' Display the Server Date Time in the ToolStripStatusLabel of the MainForm

GetServerDateAndTime.lServerDateTime.Text = Format(dtServerDateTime, "dd/MM/yyyy hh:mm:ss tt")

End Sub

Public Sub GetServerDateTime()

AddHandler tmrLocalTimer.Tick, AddressOf TimerEventProcessor

' Sets the timer interval to 1 second.

tmrLocalTimer.Interval = 1000

tmrLocalTimer.Start()

End Sub

End Class

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Hemant.Kamalakar
Software Developer (Senior)
India India
Member
An adventure in SharePoint and Microsoft in general.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralError when connect to SERVERmembercaotang25 Apr '08 - 0:04 
1. When I use connection string which Database:=(local)===>>error because program dont know what is IP Server????
2. When I connect to SERVER when time from 24h to 04h ===>>>error because time correct???

GeneralSomething newmemberSachin Mangesh Naik12 Oct '07 - 1:19 
Hey the article was okay. But its too simple stuff I suggest. Maybe you can try and update it to find out something more like the DB server time along with its Time-Zone(GMT offset) maybe.
I think that would be something more interesting for readers as well.
 
Still Thanks..

 
---
Sachin Naik.
GeneralWhere's the freakin article.membernorm .net11 Oct '07 - 21:51 
This type of submissions shows that you are a complete novice trying to help other novicesUnsure | :~ I suggest you revise the article and include a body of the problem/domain or motivation for what you are trying to solve then explain using words and code.

 

WPF - Imagineers Wanted
Follow your nose using DoubleAnimationUsingPath

GeneralRe: Where's the freakin article.memberHemant.Kamalakar11 Oct '07 - 22:00 
Thanks for ur valuable suggestion/feedbak.
I will definatly revise and update the article.
Actually I am a complete novice in Article submission.
 
Once again Thanks.....
GeneralNice ArticlememberTejesh Kamble11 Oct '07 - 21:50 
Rose | [Rose]
Thanks for very nice article.
It will help me a lot.
 
Once Again Thanks.
 
Tejesh Kamble.
Software Engineer.

GeneralRe: Nice ArticlememberHemant.Kamalakar11 Oct '07 - 22:02 
Thanks for ur valuable feedback.
GeneralNice Articlemembernikitasomaiya6 Feb '11 - 22:44 
Great one helped a lots

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 12 Oct 2007
Article Copyright 2007 by Hemant.Kamalakar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid