Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using SQLLite Database without entity Framework in ASP.NET Core Project

I want to use SQlLite Database in an ASP.NET Core project but without using an Entity Framework.

I think I should use a class which derives from DbProviderFactory & which caters to SQLLite database (something like System.Data.SqlClient.SqlClientFactory.Instance which is used for sqlServer) but I am unable to find it.

All the examples which i came across suggest me to use entity framework, but I don't want to use it.

like image 587
Abdul Rehman Sayed Avatar asked Nov 17 '16 10:11

Abdul Rehman Sayed


1 Answers

You have it in official repo: https://github.com/aspnet/Microsoft.Data.Sqlite

And here is an example: http://www.bricelam.net/2015/04/29/sqlite-on-corefx.html

EDIT: adding link content in case it disappears in the future.

The provider is built on top of the System.Data.Common contract. This contract is a very small subset of the ADO.NET provider model. Using the provider should feel very natural to anyone familiar with ADO.NET.

using (var connection = new SqliteConnection("" +
    new SqliteConnectionStringBuilder
    {
        DataSource = "hello.db"
    }))
{
    connection.Open();

    using (var transaction = connection.BeginTransaction())
    {
        var insertCommand = connection.CreateCommand();
        insertCommand.Transaction = transaction;
        insertCommand.CommandText = "INSERT INTO message ( text ) VALUES ( $text )";
        insertCommand.Parameters.AddWithValue("$text", "Hello, World!");
        insertCommand.ExecuteNonQuery();

        var selectCommand = connection.CreateCommand();
        selectCommand.Transaction = transaction;
        selectCommand.CommandText = "SELECT text FROM message";
        using (var reader = selectCommand.ExecuteReader())
        {
            while (reader.Read())
            {
                var message = reader.GetString(0);
                Console.WriteLine(message);
            }
        }

        transaction.Commit();
    }
}

Batching

The only real feature that the library adds to the native SQLite interfaces is batching. The native interfaces only support compiling and executing one statement at a time. This library implements batching in a way that should feel completely transparent. Here is an example of using batching.

using (var connection = new SqliteConnection("Data Source=hello.db"))
{
    var command = connection.CreateCommand();
    command.CommandText =
        "UPDATE message SET text = $text1 WHERE id = 1;" +
        "UPDATE message SET text = $text2 WHERE id = 2";
    command.Parameters.AddWithValue("$text1", "Hello");
    command.Parameters.AddWithValue("$text2", "World");

    connection.Open();
    command.ExecuteNonQuery();
}

Platforms:

Currently, Microsoft.Data.Sqlite works on the following platforms.

  • .NET Framework
  • Mono
  • .NET Core
  • .NET Native
  • CoreCLR
  • Windows Universal
like image 136
mattinsalto Avatar answered Sep 28 '22 01:09

mattinsalto