最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Microsoft.Data.Sqlite 9.0, SqliteConnection and SqliteTransaction - Stack Overflow

programmeradmin4浏览0评论

I am using Microsoft.Data.Sqlite.Core (9.0.2) together with SQLitePCLRaw.bundle_e_sqlcipher and I am getting issues with SqliteCommand when I have created an active transaction on the connection. The error says I need to set the Transaction property on the command object.

However my understanding is the current version of Microsoft.Data.Sqlite.Core does this automatically now.

According to this Microsoft documentation for the SqliteConnection there should be a Transaction property which I do not see. And I am definitely using the latest Microsoft.Data.Sqlite.Core from nuget.

What am I missing?

I am using Microsoft.Data.Sqlite.Core (9.0.2) together with SQLitePCLRaw.bundle_e_sqlcipher and I am getting issues with SqliteCommand when I have created an active transaction on the connection. The error says I need to set the Transaction property on the command object.

However my understanding is the current version of Microsoft.Data.Sqlite.Core does this automatically now.

According to this Microsoft documentation for the SqliteConnection there should be a Transaction property which I do not see. And I am definitely using the latest Microsoft.Data.Sqlite.Core from nuget.

What am I missing?

Share Improve this question edited Mar 12 at 3:09 Brando Zhang 28.7k6 gold badges42 silver badges70 bronze badges asked Mar 10 at 18:18 Michael TMichael T 7918 silver badges23 bronze badges 3
  • 1 Notice that on that linked page that the SqliteConnection has that Transaction property as protected internal. – pfx Commented Mar 10 at 18:54
  • I think a small reproducible example will help. In any case, you said your error is something in the command object but your linked documentation is for the sqlite connection object - so right now, just confusing. – topsail Commented Mar 10 at 19:32
  • @pfx Thanks, hadn't noticed! – Michael T Commented Mar 10 at 19:57
Add a comment  | 

1 Answer 1

Reset to default 1

The document said clearly, the Transaction is the internal usage which means you couldn't use it directly.

protected internal virtual Microsoft.Data.Sqlite.SqliteTransaction? Transaction { get; set; }

So if you want to use Transaction , I suggest you could follow below codes:

// Open a connection
using var connection = new SqliteConnection("Data Source=your_database.db;Password=your_password");
connection.Open();
// Begin a transaction
using var transaction = connection.BeginTransaction();
 
using var command = connection.CreateCommand();
command.Transaction = transaction;  
command.CommandText = "INSERT INTO YourTable (Column1) VALUES ('Value1')";
// Execute the command
command.ExecuteNonQuery();
// Commit the transaction
transaction.Commit();
发布评论

评论列表(0)

  1. 暂无评论