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

I am using Microsoft.Data.Sqlite.Core (9.0.2) together with SQLitePCLRaw.bundle_e_sqlcipher and I am ge

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();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744830187a4596036.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信