SQL Embedded in C#

Asked

Viewed 188 times

4

I have a C# application that uses a Mysql database with Entity Framework. However, the size of the database is ridiculously small, so I don’t think it’s worth having a Mysql database server just to run this application.

What would be the approach for the database to be embedded in the application, so that it can be executed without being connected to any server?

And how I would transport the database data to this new solution?

  • 2

    Hi @Felipe, I think it’s a good idea in this case to use Sqlite, it is a file (extension .db) and you use its references in C#, does not run any service, you can libraries here or also here and to visualize use the Sqliteexpert that has the free and paid version..

  • Hi Marco! Seemed like a great idea! Thanks for the tip! Hug!

4 answers

6

The solution is usually to use Sqlite. It is a built-in database and usually meets most application needs, especially if there is no direct access coming from different machines. Note that indirect access works well without problems, which is normal in web applications or that have an application server (middleware or something like).

It serves large volumes of data and access very well. You would only have problems if you had a very large amount of effectively simultaneous long scripts.

There is a driver official for Entity Framework available.

Behold when to use Sqlite.

Obviously there are other possible solutions if you do not want a built-in database, as presented in other answers. In them there will be the connection to a server, even if it is not remote.

Data transport takes place in a traditional way. Either you use client software, possibly GUI, same as any other database, and you have plenty of choice, or you do it programmatically, in which case you would have to make a connection to the unincorporated database to read the data that will be written to Sqlite, or would have to connect to a service of yours that provides the data (connecting to the server).

1


What would be the approach for the database to be embedded in the application, so that it can be executed without being connected to any server?

A good approach is the SQL Server Localdb. It is identical to SQL Server Express and uses identical Entity Framework settings in the same way.

ASP.NET MVC5 projects are usually created with Localdb being used by default.

And how I would transport the database data to this new solution?

Using the SQL Server Management Studio.

Utilize the migration assistant.

Here is the Mysql plugin.

  • Thank you, Gypsy! I think it is the most appropriate option. Can you tell me if, when creating an installation file, I can integrate this feature? Or the customer would have to install the part?

  • 1

    Thanks Gypsy! Once again helping me a lot!

  • Gypsy, your idea worked very well on my computer. It creates some . mdf files and everything works fine, even in the installer. However, when I install it on another PC, at first it gives a talking error that can’t connect to Sqlserver. I installed the SQLLOCALDB.MSI file and the error changed. It says it cannot create the file as it has already been created... Have any tips on how to make it work with the installation on any computer?

  • You have made sure that Localdb is installed on another computer? Here is the separate installer, for 32 and 64 bits.

1

0

The fact is to know if you really need to persist/save this data. If so, it will have to be somewhere. That part is yesterday we take the decision on persistence.

Often, using a simple database like Mysql is more advantageous - as simple and quick implementation - than trying a more "creative solution".

So the question is: do you need to persist data?

If so, what advantage do you see in not having a database? What problem will you solve by not having a persistence service like a Mysql?

If not, you can leave for a solution In Memory, how to instantiate a System.Data.DataSet, in it create all the tables and relationships that your application needs. So make a first Seed of data for your application to start working. This is an option where, if your application is recycled, all data will be cleaned and started from scratch.

Important also consider that your application already exists and is already working with a database. Is it worth removing it? Is ROI - return on investment - interesting? Because removing a database, already implemented with EF, will cost the developer a few hours to adapt.

  • Hi Thiago! The data is for consultation only. It has not yet been installed in the client and I want to avoid installing and configuring the database. However, as the data is already in Mysql I would like a simple way to transport them. I’ve seen some similar approaches but never used...

  • @Felipebulle, use a local database. The reply of the Gypsy are very convenient.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.