-2

I 'm working on a project that has a database which is in the form of a text file. This project is a web service which answers required data from different users by web method calls. The text file for the database would be increasing in size and maybe have the size more that 100 MB How can I load this data text file in my whole application in asp.net to the memory and for every request from different users get the data from loaded data in memory? In addition to this is, it possible to add more data to this database variable in the memory and then save the changed data in the memory to the file ?

Thank you I appreciate any help.

mason
  • 31,774
  • 10
  • 77
  • 121
user3173814
  • 197
  • 1
  • 1
  • 8
  • 1
    What specific problem are you facing at this moment in your mission to implement this yourself? You have asked several questions here, some of which are very broad. – eddie_cat Oct 28 '14 at 13:52
  • Hi, Thanks for your concern. I have created a class named database and a method named readData(). for every request from different users I have to create database object and call readData. I want to have an instance of my database variable in memory and access that in every requests, Is it possible with Global.asax ? – user3173814 Oct 28 '14 at 13:54
  • 1
    You're using a text file as a database?! May I ask why? – Tanner Oct 28 '14 at 13:54
  • Sure, this text file is a collection of hashes created from processing some files, all of these hashes should be loaded in the memory for querying from this hashes, It was not possible to save this data in database – user3173814 Oct 28 '14 at 13:59

1 Answers1

2

You can easily read the text file into memory. You'd probably want to put this in your Application_Start function in your Global Application Class (global.asax).

Application["TheData"] = System.IO.File.ReadAllText(Server.MapPath("~/App_Data/Database.txt"));

If you need to re-initialize the data, just run that line of code again, perhaps in response to a file upload.

However, now you've just got a giant string in memory. You'll need to probably process that into some sort of structured class (and store that, rather than the string), then have methods for answering the questions. It might look something like this:

MyDatabase db = MyDatabase.LoadFromText(File.ReadAllText(Server.MapPath("~/App_Data/Database.txt")));
Application["TheData"] = db; //probably better to make it a one liner

By the way, a giant text file is a bad idea. Instead of a text file, use an actual database. They're meant for holding data that can change, and you can come up with a proper relational structure.

mason
  • 31,774
  • 10
  • 77
  • 121
  • Thank you mason, So I have to put this data loading in Application_Start and for different users I can Access this loaded data and does not need to load it again, am I right ? – user3173814 Oct 28 '14 at 14:04
  • @user3173814 Application_Start is a [function that runs when your application starts](http://www.techrepublic.com/article/working-with-the-aspnet-globalasax-file/). The [Application class](http://stackoverflow.com/questions/5096544/application-vs-session-vs-cache) is a shared object between all user sessions. It's similar to Session, but shared by everyone. – mason Oct 28 '14 at 14:07