Solved

dataset as a cache in C# windows application

Posted on 2006-06-20
22
537 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
  • 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
 

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 500 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows phone Development 11 29
Handling arrays 14 33
asp.net bundle 8 36
dynamic menu in asp.net c# 11 29
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now