Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Database access time

Hi there, i have a program that accesses a database, and does this at various times, however when it goes to access the database the program stops responding until its done, someone told me about DO EVENTS. will this help speed up the process or allow me to continue working in the program whil the database check is completed? Thanks
[334 byte] By [vchatlive] at [2007-11-11 10:08:05]
# 1 Re: Database access time
It may or may not depending on what the cause of the delay is. If you are opening the database every time you access it then try leaving it open until the application terminates. same thing for any recordsets you need open to access the database. If you need to make sure you have the latest data in your recordsets you can do a refresh on each recordset. This will take less time than recreating the recordset each time. Of course this won't work if every time the selection criteria for any recordset changes. Then you would have to close and reopen the recordset. Now if accessing the database entails looping through lots of records in your various recordsets then the DoEvents command strategically placed within each loop would help your program seem more responsive; but if the user should not be doing anything during this database access process then you would be better off with a progress bar dialog so they know that something important is going on and need to wait for it to finish.
Ron Weller at 2007-11-11 17:23:13 >
# 2 Re: Database access time
basically what this is for is a online/offline contact list. Users are stored in a database, and when the timer fires, it has to download the users contact list, then check each contact to see if there status is online or offline. would seeing the code possibly help:? Thanks!
vchatlive at 2007-11-11 17:24:13 >
# 3 Re: Database access time
Yes, that would help.
Ron Weller at 2007-11-11 17:25:11 >
# 4 Re: Database access time
rs.Open "select `contact` from `" & frmLogin.Text1.Text & "`", conn
rs.MoveFirst
Do Until rs.EOF = True
rs2.Open "select * from `basicreg` where `username` = '" & rs.Fields("contact").Value & "'", conn2
If rs2.Fields("status").Value = "online" Then
Set nodx = TreeView1.Nodes.Add("online", tvwChild, rs.Fields("contact").Value, rs.Fields("contact").Value)
Else
Set nodx = TreeView1.Nodes.Add("offline", tvwChild, rs.Fields("contact").Value, rs.Fields("contact").Value)
End If
rs2.Close
rs.MoveNext
Loop
conn2.Close
rs.Close
conn.Close
vchatlive at 2007-11-11 17:26:19 >
# 5 Re: Database access time
connection strings are right above that and so are the main node settings but this is the part that takes forever.
vchatlive at 2007-11-11 17:27:18 >
# 6 Re: Database access time
I noticed that you have two connection objects, is this because the two tables are in different databases?
Ron Weller at 2007-11-11 17:28:16 >
# 7 Re: Database access time
yes, 2 seperate databses, each one has a table that needs to be checked, one is for the user status and the other is the user contact lists. so it looks first at User1's contact list and then goes through it and finds User 2 and 3 in the database and checks there status.
vchatlive at 2007-11-11 17:29:20 >
# 8 Re: Database access time
Ok, first thing I would do is to break this code up into seperate routines. When the application starts up create the connection objects and leave them open until the application terminates. You should make them global to the application and you should have one for each database. All routines would then use those connection obnjects whenever they are needed. You don't want the connection time added to your routine every time you check the online/offline status. Next I would also create a global recordset object for the contact list table and initialize/open it when the user logs in. Keep it open until the user logs off or the application terminates. Then I would have two routines, one that initialy loads the treeview control with the entries from the contact list and use 'basicreg' to places the node in the approprate location based on status. Basically the code you have now but with the recordset and connection objects already open, and no close at the end. Next I would create a routine to be called by the timer; it simply runs through the list just like the load routine but instead of adding them to the TreeView control you can simply change the node location based on the status from the 'basicreg' table.
Ron Weller at 2007-11-11 17:30:23 >
# 9 Re: Database access time
makes sense, ill give that a shot, i have another question, do you know how i can get a users REAL IP not there network IP, this will be used over the internet, and we need the ability to send messages to individual people, so if you and i are talking, when you login, it shoudl grab your IP and save it in the database. so when i click your name it knows what IP to send the message to. Thanks so much!!
vchatlive at 2007-11-11 17:31:17 >
# 10 Re: Database access time
also if possible is there anyway i can talk to you on a messenger about this? got a couple quick questions and you seem to know alot about this stuff thanks!
vchatlive at 2007-11-11 17:32:17 >
# 11 Re: Database access time
I just had another Idea. You could create a simple routine that just takes a contact and adds it to the TreeView control. Then you could add an error check to the routine that tries to reposition the node to the correct status location. If the node is not found then call the simplified routine with the contact and status and it will add the node. Now you can use the same routine for both initial loading of the treeview as well as updating the status. This way whenever a new contact is added the status update routine won't find it and will call the new add function, automatically adding any new contacts. Of course deleting old contacts is another situation altogether.
Ron Weller at 2007-11-11 17:33:16 >
# 12 Re: Database access time
so far the way i have it going if someone adds or removes a contact when the routine fires on the timer1_timer control, it automatically updates. so that parts working right now with only a couple contacts its pretty quick and doesnt bother much but when someone get say 25+ contacts its going to start taking a while im trying to avoid problems later down the road, any ideas? the ones you gave me so far are awesome im already working them into the program just wanna make sure i have this ready so we can start using it
vchatlive at 2007-11-11 17:34:27 >
# 13 Re: Database access time
Is this program the only way they can add/delete people to their contacts list?
Ron Weller at 2007-11-11 17:35:23 >
# 14 Re: Database access time
yes this program will be the only way they access the database ( and its done entirely through the program, Thank you!
vchatlive at 2007-11-11 17:36:28 >
# 15 Re: Database access time
That makes is alot easier. In the delete contacts person routine you can alos delete the contact node from the treeview control. Since it gets reloaded every time they log in, it will always be correct when it starts off. If the node is not found for you to delete it could be that they are trying to delete a newly added contact that has not been added to the treeview control yet by the timer event; so basically you can ignore not found errors.
Ron Weller at 2007-11-11 17:37:20 >
# 16 Re: Database access time
yeah i think we got that pretty much setup, only thing im noticing is as more contacts are added it does in fact take longer to do the sequence, but i guess thats something i can work out later, for now ive used a few suggestions of yours and so far it IS in fact a little quicker, ive got bigger fish to fry now like finding a users IP for instant messaging! if you know anything about that, give me a shout i havea few questions i may start a new thread for it. Thanks!
vchatlive at 2007-11-11 17:38:21 >