You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When doing a multi-statement transaction using Microsoft.Data.Sqlite that causes records to be affected, the provider seems to add the number of records affected on to the total for any statements that don't affect any records. This keeps happening for subsequent statements until there's another statement which affects records, at which point that new number is added for this statement and all subsequent statements that don't affect any records. The result is an incorrect number of records affected at the end of the statement. The repro code below shows this issue; when a DROP statement is added, records affected is incorrectly incremented by 1, the number of records affected by the most recent statement that affected records. When a CREATE statement is also added the results again incorrectly increments by 1.
Your code
usingMicrosoft.Data.Sqlite;namespaceSqliteBugRepro;internalclassProgram{privateconststring_dbFile="testDb.sqlite";privateconststring_createAndInsert="CREATE TABLE foo(bar TEXT NOT NULL); CREATE TABLE xyz(aaa TEXT NOT NULL); INSERT INTO foo(bar) VALUES('baz'); INSERT INTO foo(bar) VALUES('baz2');";privateconststring_drop="DROP TABLE xyz;";privateconststring_create="CREATE TABLE xyz(aaa TEXT NOT NULL);";staticvoidMain(string[]args){Console.WriteLine("Creating new SQLite DB.");CreateSqliteDb();intrecordsAffected;Console.WriteLine("Doing DB commands (no DROP).");recordsAffected=DoCommandsNoDrop();Console.WriteLine($"Records affected: {recordsAffected}");// 2Console.WriteLine("Creating new SQLite DB.");CreateSqliteDb();Console.WriteLine("Doing DB commands (with DROP).");recordsAffected=DoCommandsWithDrop();Console.WriteLine($"Records affected: {recordsAffected}");// 3 (???)Console.WriteLine("Creating new SQLite DB.");CreateSqliteDb();Console.WriteLine("Doing DB commands (with DROP and CREATE).");recordsAffected=DoCommandsWithDropAndCreate();Console.WriteLine($"Records affected: {recordsAffected}");// 4 (???)}privatestaticvoidCreateSqliteDb(){File.WriteAllBytes(_dbFile,[]);}privatestaticintDoCommandsNoDrop(){varconnStringBuilder=newSqliteConnectionStringBuilder{Pooling=false,DataSource=_dbFile};usingvarconn=newSqliteConnection(connStringBuilder.ConnectionString);conn.Open();usingvarcmd=conn.CreateCommand();cmd.CommandText=$"{_createAndInsert}";usingvarreader=cmd.ExecuteReader();do{varcurrentRecordsAffected=reader.RecordsAffected;}while(reader.NextResult());returnreader.RecordsAffected;}privatestaticintDoCommandsWithDrop(){varconnStringBuilder=newSqliteConnectionStringBuilder{Pooling=false,DataSource=_dbFile};usingvarconn=newSqliteConnection(connStringBuilder.ConnectionString);conn.Open();usingvarcmd=conn.CreateCommand();cmd.CommandText=$"{_createAndInsert}{_drop}";usingvarreader=cmd.ExecuteReader();do{varcurrentRecordsAffected=reader.RecordsAffected;}while(reader.NextResult());returnreader.RecordsAffected;}privatestaticintDoCommandsWithDropAndCreate(){varconnStringBuilder=newSqliteConnectionStringBuilder{Pooling=false,DataSource=_dbFile};usingvarconn=newSqliteConnection(connStringBuilder.ConnectionString);conn.Open();usingvarcmd=conn.CreateCommand();cmd.CommandText=$"{_createAndInsert}{_drop}{_create}";usingvarreader=cmd.ExecuteReader();do{varcurrentRecordsAffected=reader.RecordsAffected;}while(reader.NextResult());returnreader.RecordsAffected;}}
Stack traces
Microsoft.Data.Sqlite version
9.0.2
Target framework
.NET 9.0
Operating system
Windows 10
The text was updated successfully, but these errors were encountered:
Bug description
When doing a multi-statement transaction using
Microsoft.Data.Sqlite
that causes records to be affected, the provider seems to add the number of records affected on to the total for any statements that don't affect any records. This keeps happening for subsequent statements until there's another statement which affects records, at which point that new number is added for this statement and all subsequent statements that don't affect any records. The result is an incorrect number of records affected at the end of the statement. The repro code below shows this issue; when a DROP statement is added, records affected is incorrectly incremented by 1, the number of records affected by the most recent statement that affected records. When a CREATE statement is also added the results again incorrectly increments by 1.Your code
Stack traces
Microsoft.Data.Sqlite version
9.0.2
Target framework
.NET 9.0
Operating system
Windows 10
The text was updated successfully, but these errors were encountered: