0

I am working on a small windows forms program that reads data from a local database, which I have created by following this guide.

I have populated these tables with data using the Designer in Visual Studio, and there is no ability (nor will there ever be) for the program to make changes to this database at run-time, as they represent static, known data -- I could get identical results if I hard-coded the instantiation of each corresponding table row object in a constructor somewhere.

When I have Visual Studio build my solution, it generates two files -- the .exe that opens the Form, and a .mdf file with the database tables.

Two related questions -- does it even make sense to use a database with this kind of read-only data? And if so, is there a way to combine the .MDF file into the .exe? Again, there is zero need to ever modify the data, so I wouldn't think that the fact that you can't modify .exes need prevent this.

Raven Dreamer
  • 6,940
  • 13
  • 64
  • 101
  • 1
    Depends how you use the database; If you are using it as a flat information store only and simply loading all the data into memory once then as you suspect a database is a poor choice. An alternative would be to embed some XML as a resource: http://stackoverflow.com/questions/2820384/reading-embedded-xml-file-c-sharp – Alex K. Mar 28 '17 at 14:41
  • The database file can only be processed by a database engine, so no, there's no stuffing that into your executable. You could take a look at lighter-weight options like [SQLite](https://system.data.sqlite.org/) if your data is substantial enough that storing it as tables still makes sense (as opposed to, say, XML resources). – Jeroen Mostert Mar 28 '17 at 14:42
  • @AlexK. It's not flat, but XML (or JSON, I guess?) resources files probably make as-much or more sense. That's a useful link, thank you. – Raven Dreamer Mar 28 '17 at 14:56
  • You can make an two accounts in SQL Server. One with Full Access and another with Read Only Access. – jdweng Mar 28 '17 at 15:20

1 Answers1

1

Yes you can do this. But first you need to construct your dataset. I would create your data in SQL or similar then export it to XML along with an XSD schema.

Then in Visual Studio, add a DataSet object to your project.

Then you can talk to the DataSet object and use the XSD and XML to populate it.

https://msdn.microsoft.com/en-us/library/atchhx4f(v=vs.110).aspx

To keep this all within the .EXE, embed your XML data into a Resource file, then access it via the Resources static class.

Ross Miller
  • 656
  • 3
  • 9