[英]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. 我想在ASP.NET Core项目中使用SQlLite数据库,但不使用实体框架。
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. 我想我应该使用一个派生自
DbProviderFactory
的类,它适合SQLLite数据库(类似于用于sqlServer的System.Data.SqlClient.SqlClientFactory.Instance ),但我无法找到它。
All the examples which i came across suggest me to use entity framework, but I don't want to use it. 我遇到的所有示例都建议我使用实体框架,但我不想使用它。
You have it in official repo: https://github.com/aspnet/Microsoft.Data.Sqlite 你有正式的回购: https : //github.com/aspnet/Microsoft.Data.Sqlite
And here is an example: http://www.bricelam.net/2015/04/29/sqlite-on-corefx.html 这是一个例子: 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. 提供程序构建在System.Data.Common合同之上。 This contract is a very small subset of the ADO.NET provider model.
此合约是ADO.NET提供程序模型的一个非常小的子集。 Using the provider should feel very natural to anyone familiar with ADO.NET.
对于熟悉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. 库添加到本机SQLite接口的唯一真正功能是批处理。 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. 目前,Microsoft.Data.Sqlite适用于以下平台。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.