borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (SORT MSHflexigrid)Next Topic (Formatted cells in flexgrid) New Topic New Poll Post Reply
AndreaVB Forum : Database : Using getdate() from server
Poster Message
Aeric
Level: Sage


Registered: 16-06-2003
Posts: 53

icon Using getdate() from server

Hi, it has been long time i don't visit this forum since i was busy with my exams. Now I am working as a junior programmer. Our company program is written by my senior using VB6 and SQL server.

To show the server time in client side which will update every seconds, the code is as follow:

Private Sub Timer1()
Dim rsTime As ADODB.Recordset
Dim sSQL As String

sSQL = "SELECT getdate() as ServerTime"
Set rsTime = ExecuteSelectSQL(sSQL)
lblCurrentTime.Caption = rsTime!ServerTime
End Sub

*Note: ExecuteSelectSQL is a function to execute the SQL statement

My question is:

Does this code affect the performance of the program since it executes every second to retrieve value from the server?

Can i say this coding has its advantage since it will show error message if the client machine is disconnected from the server database?

Can i say that it may consume less processing and memory if i only use one timer in MDIForm instead of using a timer in every child form?

Thank you.

07-10-2004 at 01:31 AM
View Profile Send Email to User Show All Posts Visit Homepage ICQ | Quote Reply
GeoffS
Level: VB Lord


Registered: 29-09-2004
Posts: 536
icon Re: Using getdate() from server

Hi Aeric,
The answers - and this is my opinion only - are:
Yes
No
Yes

But the real question is why do you need to fetch the time every second. Would it not be better to set the time on the Client PC at user log-in by adding

Net Time \\server_name /set /yes

to the user login script??
Then the program can ask the local PC for the time whenever it wants to use it.



____________________________
multi-tasking - the ability to hang more than one app. at the same time.

07-10-2004 at 08:26 AM
View Profile Send Email to User Show All Posts | Quote Reply
Aeric
Level: Sage


Registered: 16-06-2003
Posts: 53
icon Re: Using getdate() from server

Thanks Geoffs for your reply.

My point of view to my questions are:

Q1: A simple select statement will not reduce the performance so much but sure it will if many clients are connected to the server. Normally this code

lblCurrentTime.Caption = FormatDateTime(Now,vbLongTime)

is used if the program is running in standalone Client or locally. The getdate() is used in similar concept by my senior I think. That's why I post this message to improve the program's perfomance.

Q2: To indicate that the client is connected to the server or not is not important. Error message will shown when the client need to retrieve data from the server and fails. I think the performance is more to concern. Right?

Q3: First I think want to get the server time once when the program starts and then compare the difference between the server time and local time using DateDiff. I can use the difference value to adjust the time. i.e Minus the difference fron the local system time.
e.g diftime=DateDiff("s",Now,ServerTime)
      lblCurrentTime.Caption = FormatDateTime(Now - diftime,vbLongTime)

The purpose/reason I want to get the server time is I want to write a Alarm/Reminder program which will alert all the client to check the "to do list".

I don't know how to use this script:

Net Time \\server_name /set /yes

Is it append together in ADO connection string? Please explain in details or give example since I have never use the so called "user login script". I am very much appreciate your help.

07-10-2004 at 09:03 AM
View Profile Send Email to User Show All Posts Visit Homepage ICQ | Quote Reply
GeoffS
Level: VB Lord


Registered: 29-09-2004
Posts: 536
icon Re: Using getdate() from server

Hi Aeric,
Without knowing the precise nature of your company's setup and requirements it is impossible for me to say that the following is necessarily the best advice. But for what its worth here are my thoughts on your problem -
Last question first -
User Log-In Script.
When the user of a networked PC first logs on with the Windows Login screen his/her user name and password are checked by the Server computer for the Domain/Workgroup to which they belong. At this point the Server can run a user login script which, among other things, can set Network drives for the client PC and other environment variables. This file is saved as a Batch File (.bat) on the server. If you look on the shared folders of the server to which you connect you may possibly see a folder called "NetLogon" - you may find a sample script there, otherwise talk to the system administrator of your Network.
The point here is that adding the above line to a users network logon script will force the Client PC to synchronise time with the Server PC everytime a user logs in, which presumably will be at least once a day. Modern computers can keep acurate enough time over a 24 hour period so I would suggest that it would be perfectly safe to then use the local computers time for your needs. This not only saves processor time but also reduces the amount of traffic on the network - think how many users are sending time requests every second!! Also, unless your Server computer is connecting to a proper TimeServer on a regular basis who's to say that the time on that machine is exact?
So that, I think also answers Q1. By getting the time locally rather than over the Network the performance will improve anyway, but depending on how busy your Network is it could be quite a dramatic change.
Q2. Right - You answered yourself.
Q3. If accuracy of time compared to Server time is that important then yes, run a check occasionally, but once a second seems a bit much. No point using processor cycles to calculate time differences though, just apply the Server time directly to your local PC time (Time = ServerTime) But if you can live with the time being out by a few seconds then sychronisation by the login script has got to be the best way to go.


____________________________
multi-tasking - the ability to hang more than one app. at the same time.

07-10-2004 at 11:41 AM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Using getdate() from server

quote:
Does this code affect the performance of the program since it executes every second to retrieve value from the server?


When I read about ADO while ago, I remember once some MS ADO Guru said that ADO recordset are very expensive to create so we need to avoid creating them whenever possible. In your code you create ADO recordset every second, so I think it surely does affect a performance, and especially in large network.

So, listen to Geoffs's advice and use Net Time command, and for your question how to use it, check this.

____________________________
If you find the answer helpful, please mark this topic as solved.
07-10-2004 at 08:46 PM
View Profile Send Email to User Show All Posts | Quote Reply
GeoffS
Level: VB Lord


Registered: 29-09-2004
Posts: 536
icon Re: Using getdate() from server

Heh - that's good Goran.  
I never thought of using it that way - Ta.  

____________________________
multi-tasking - the ability to hang more than one app. at the same time.

08-10-2004 at 08:02 AM
View Profile Send Email to User Show All Posts | Quote Reply
Aeric
Level: Sage


Registered: 16-06-2003
Posts: 53
icon Re: Using getdate() from server

Thanks to Geoffs and Goran

quote:
Net Time \\server_name /set /yes


Q1: I can't try the user login script and net time command coz the server has password and I get access denied error.
Should I use a shell command to login as a user before using net time command? How?

Q2: I have try using command prompt and VB to get the time from other computer in the network which has no password and output as text file.

Private Sub Command1_Click()
Shell "cmd.exe /c net time \\Server >> " & App.Path & "\date.txt", vbHide
End Sub

Can i directly get the time value and pass it to my application? What does the parameter /c refer to?

Q3: Adding /set /y in the command will force the sytem to synchronise with the server. It may be better to check the time before synchonization and pop up a message box like below?

Msgbox "Your system time is not synchronized with the server time." & Chr(13) &
"Do you want to synchronise the time now?", vbYesNo

Thanks a lot.  
08-10-2004 at 08:45 AM
View Profile Send Email to User Show All Posts Visit Homepage ICQ | Quote Reply
AndreaVB Forum : Database : Using getdate() from server
Previous Topic (SORT MSHflexigrid)Next Topic (Formatted cells in flexgrid) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder