Link to home
Start Free TrialLog in
Avatar of engg
engg

asked on

dataset as a cache in C# windows application

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");
   }
}
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I don't understand your question.  You can keep the DataSet in memory indefinitely, without having to go back to the database.

Bob
Avatar of engg
engg

ASKER

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.
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
Avatar of engg

ASKER

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.
>>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
ASKER CERTIFIED SOLUTION
Avatar of vo1d
vo1d
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
is it possible that to instances of your app could be started at the same time?
Avatar of engg

ASKER

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?
Avatar of engg

ASKER

>>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.
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.
Avatar of engg

ASKER

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?
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?
Avatar of engg

ASKER

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?
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?
Avatar of engg

ASKER

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.

yes, thats what you should doublecheck.
Avatar of engg

ASKER

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?
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
why do you want to keep the application open?
Avatar of engg

ASKER

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.
Avatar of engg

ASKER

That was because Bob said 'if nothing is keeping the application open'.
Was just curious if that could be done.