?
Solved

dataset as a cache in C# windows application

Posted on 2006-06-20
22
Medium Priority
?
588 Views
Last Modified: 2010-04-16
Hello,

I am using a dataset to store infrequently changing data in a C# Windows application. I am not sure how long this data can be stored in the dataset without accessing the database again. Please see below the relevant portion of the code. When the project is run, the stored data can be accessed without connecting to the database. But this project will be run a lot of times during a day. I want to get the data from the database only when the project is run the first time and then on subsequent runs, I dont want to connect to the database. How should I do that? Please help. It's urgent. Thank you.

static void Main()
{
   cache c = new cache();
   // At this point, I can access the stored data without connecting to database.
}

public static ds dataset;
public cache()
{
   if (dataset == null)
   {
       dataset = new ds();
       OdbcDataAdapter odba = new OdbcDataAdapter("select * from product", odbcConn);
       odba.Fill(dataset, "products");
   }
}
0
Comment
Question by:engg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
  • 4
22 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16953408
I don't understand your question.  You can keep the DataSet in memory indefinitely, without having to go back to the database.

Bob
0
 

Author Comment

by:engg
ID: 16953512
Thanks Bob.

static void Main()   //windows app.
{

}

Every day, during the first execution of 'Main()' , I want to connect to the database and store the data in the dataset. On the subsequent executions of 'Main()'  throughout the day, I dont want to connect to the database, I want to get data from that dataset. is it possible?  

Please let me know if you got what I am saying.
Thank you.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16953564
Let me see if I understand what you are saying:

   1) Are you stopping and restarting the application to execute Main multiple times?

   2) If you are, then you would need to save/restore the data

   3) If you aren't, then why are you calling Main multiple times from the same running instance?

Bob
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:engg
ID: 16953827
Thanks Bob.

even though it's Windows application, there are no WinForms in it.   It has been built already and does NOT have to be rebuilt (by going to menu - Build -> Build Solution).

Every time a request is made to do the task (that this application is supposed to do) , the control goes to the 'Main()' method. Once the task is done,  the control reaches the end of 'Main()'.

So the 'Main' method is executed as many number of times as we need to perform the task.
So i guess that's what is meant by 'The application is stopped and restarted'.

So how would i save the data then so that I don't have to connect to database every time the task is performed?

Please let me know. Thank you.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16954031
>>So i guess that's what is meant by 'The application is stopped and restarted'.
Yes, if you are reaching the end of Main, and nothing is keeping the application open, then it will exit (stopped)

Do you have more than one application in play?  One that calls this application with the Main method?

Bob
0
 
LVL 11

Accepted Solution

by:
vo1d earned 2000 total points
ID: 16954062
write that data to a file with teh dataset method WriteXml.
0
 
LVL 11

Expert Comment

by:vo1d
ID: 16954116
is it possible that to instances of your app could be started at the same time?
0
 

Author Comment

by:engg
ID: 16954151
Thank Bob, vo1d.

>> Do you have more than one application in play?  One that calls this application with the Main method?
1) yes, I call this (Win) application from another app (ASP.NET C# web app) using System.Diagnostics.Process.  
2) and Also, I have scheduled it (Win app) to run every morning using Control Panel -> Scheduled tasks.

So every time it is called, the control goes to its Main() method.

>> if you are reaching the end of Main, and nothing is keeping the application open, then it will exit (stopped)
How could we keep the application open? Is there any way to do that?
0
 

Author Comment

by:engg
ID: 16954182
>>is it possible that to instances of your app could be started at the same time?

Thanks vo1d. How would it solve this prob?
I would like to do that if it does. But if we run 2 instances at the same time, it will be doing the same operations twice. and the 'Main()'  of this app. takes long to execute.
0
 
LVL 11

Expert Comment

by:vo1d
ID: 16954201
yes you could, but then you would have to make the scheduling by your own.
scheduled task is fine for that, i would not recommend implementig an own scheduling mechanism.
does that scheduled app AND your asp started instance run on the same machine?
if so, you would have to ensure that the file is only written by teh file, which is started at first.
for that, you could use a mutex which is set if the file is written so that other instances know, that they have to wait for fileloading until the mutex is deleted.
to check, if the file is from the day before, you could check its creation date.
0
 

Author Comment

by:engg
ID: 16954320
Thanks Vo1d.
All instances run on the same machine, but I am already making sure that only one instance runs at a time.  I guess then that's easy for your solution (dataset WriteXml) to work , right?
0
 
LVL 11

Expert Comment

by:vo1d
ID: 16954381
ok, then you dont need the mutex thing.
to write your data, just use
dataset.WriteXml("filename");

and check the date, if it is the next day,  before your write your data to the file.
but i only recommend writing to file if you are not dealing with a huge amount of data.
why cant you always connect to the database? do you have a low connection?
0
 

Author Comment

by:engg
ID: 16954435
thanks vo1d.

The connection speed is fine, but the data I want to store is static for the whole day and it's not huge.

I think reading from a dataset or reading from a XML file on the local hard drive is ALWAYS going to be faster than reading from a non-local database. (no matter how huge the data is)

Is that correct?
0
 
LVL 11

Expert Comment

by:vo1d
ID: 16954527
not in every case.
if you have alot of instances, which are reading data from a file at the same time, the system is going down. we noticed that on some of our terminal server.
you have to keep in mind, that you not only have file operations if you read from your file, the system has also io operations like the pagefile, and dll binding operations.
about how many instances of reading apps for that data do we talk?
0
 

Author Comment

by:engg
ID: 16954635
Ok.
There could be max. 15 reading app. running at the same time.   I guess now I have to compare efficiency of these two -
15 users reading a file concurrently and 15 users accessing the same data from the database concurrenly.

0
 
LVL 11

Expert Comment

by:vo1d
ID: 16954666
yes, thats what you should doublecheck.
0
 

Author Comment

by:engg
ID: 16954718
Thanks a lot vo1d for your help. I will accept your answer in a while.

Am just waiting for Bob.
>> if you are reaching the end of Main, and nothing is keeping the application open, then it will exit (stopped)
How could we keep the application open? Is there any way to do that?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16954774
On the surface it doesn't sound like you could keep the application open.  If you kept it open, would you look to put the application to sleep until a specified time, within an infinite loop?  What would be the exit condition?

Bob
0
 
LVL 11

Expert Comment

by:vo1d
ID: 16954806
why do you want to keep the application open?
0
 

Author Comment

by:engg
ID: 16954854
Ok.
Thanks a lot, Bob and vo1d, for your help. I will accept vo1d's solution as an answer. Both helped me make my concepts clear.
0
 

Author Comment

by:engg
ID: 16954879
That was because Bob said 'if nothing is keeping the application open'.
Was just curious if that could be done.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question