Question

I am developing a .NET (C#) application with a binary file format. The plan is to migrate to binary format to an SQLite database file which is easier to maintain.

I have never used this before so I have a few questions.

  1. The SQLite implementation of http://sqlite.phxsoftware.com/ contains a LINQ provider. I have played with it locally, but I somehow have to incorporate this into something a team of developers can use. I can place the database file on my PC in a folder which is included in the source repository, but eventually it should end up in the clients AppData folder. How can I achieve this using a generated database file? When I create the database file manually I can easily tell the application where to store it.

  2. I have a fair knowledge of SQL queries, so my initial plan was to create a library which does a port of a DataModel to the database using SQL queries. The downside of this is that when a field changes or is added I have to update a lot of things: The database create query, the database update or alter queries, the DataModel. From what I gather I can create a database using the designer, but when my application is updated to version 2 next month and my database changes I want to migrate the user's existing database to the new version. Doing this manually I can write update queries and work with some sort of incremental database counter to check this, but how does this work using a generated database?

So is it better, easier and safer to use SQL queries directly, or can someone point me to a decent usable use-case of using a generated database?

Thanks.

Was it helpful?

Solution

For your first question I can suggest you to make your application in ASP.Net or make use of webservice (the second is more suitable for your objectives). For webservices I think you should code a session in the call of a method like this:

...
[WebMethod]
public DataSet GetData(object id, string sessionId)
{
   try
   {
      DataSet ds = null;
      CUser user = Global.GetSession(sessionId);

      lock
      {
         //Your code to get data from the database
      }
      return ds;
   }
   catch(Exception ex)
   {
       throw;
   }
}

For your second question I suggest to use FluentNhibernate. I've used FluentNhibernate and is very easy to model your database and even generate it. This is just a glimpse of the code used to configure the SQLite implementation;

private static ISessionFactory CreateSessionFactory()
{
  return Fluently.Configure()
    .Database(
      SQLiteConfiguration.Standard
        .UsingFile("firstProject.db")
    )
    .Mappings(m =>
      m.FluentMappings.AddFromAssemblyOf<Program>())
    .ExposeConfiguration(BuildSchema)
    .BuildSessionFactory();
}

private static void BuildSchema(Configuration config)
{
  // delete the existing db on each run
  if (File.Exists(DbFile))
    File.Delete(DbFile);

  // this NHibernate tool takes a configuration (with mapping info in)
  // and exports a database schema from it
  new SchemaExport(config)
    .Create(false, true);
}

You can see the FluentNhibernate wiki page and there's a introductory implementation of this framework. Finally to admin your SQLite database I suggest you to use SQLite Administrator.

alt text

OTHER TIPS

Firstly, System.Data.SQLite implements an ADO.NET Provider first and foremost. Your connection string's data-source IS the file you want to manipulate, however you WILL run into issues with SQLite if you're trying to access it from multiple places. There are workarounds documented though to do with journaling & locking levels.

To "create" the db on the user's pc, you can either store an empty db in a resource and copy it to the location, or open a new db and run a create script to make the tables/etc.

Secondly, STOP. If you're going to go above and beyond the call of ADO.NET to abstract your data layer, just use an existing DB Abstraction, don't build your own.

I suggest NHibernate, there's a driver for SQLite too!

I'm not sure I understand question 1. Does your program need to take a pre-generated database file and put it into the client's AppData folder, or does your program need to generate a new database file when it is run for the first time on the client machine and then store that newly generated database file in the AppData folder.

As for the second question, I don't understand what you are trying to do. Could you please clarify and/or provide an example situation?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top