c# - Dependable insert with SQLite -
I am writing a program in C #, for which data must be included in several tables. The first insert gives a final entry queue, which in turn is used to put one on the other tables. In pseudo code:
insert in the tablefood (call 2, call3, call4, etc) value (@blue2, @bl3, @ black4);
Immediately after joining, I get the last entry ID:
SELECT last_insert_rowid () AS RowId;
I retrieve the Rowid column in a detainee and store it in an int "roid". In the end, I include some more tables:
INSERT (RowId, @BSomeMoreBla) in the tablebar (FooId, Col2) values;);
Question : If possible, I want to do this bunch (and a selected road) to insert 1 transaction to prevent some half-saved data. Is it possible to use transactions and what would be the preferred ways?
A SQLite transaction? C # 's system. Transactions namespace? A nuclear database A third way of multi-inserting ...?
Rair helped me a lot on my own path , But I think that the solution I was looking for has got to me. Those who might be interested, here my code is probably right, but completely legal and usable code):
Depending on the PK of TBL via an auto-growing PK, TB2 and TBL3FK in NB. Tbl1 Land. If an exception occurs, should behave just like a decent transaction ;-). Only if C Ommit () is executed without errors, the whole bunch is inserted, even if there is a selection in the middle of the road.
Try {transaction = connection.BeginTransaction (); Command = new SQLiteCommand (); order. Transaction = transaction; Command. Enter into the COMANDTEX = "TBL1 (data) value ('blog')"; Command.ExecuteNonQuery (); Command = new SQLiteCommand (); order. Transaction = transaction; Command.CommandText = "select last_insert_rowid ()"; RowId = Convert toInt64 (command.ExecuteScalar ()); Command = new SQLiteCommand (); order. Transaction = transaction; Command. Enter the COMANDTEX = "TBL (ID, TBLIID) values (2, @ ORID)"; Command. Parameter. Add (new SQLTapMeter ("@ line IID", line ID)); Command.ExecuteNonQuery (); Command = new SQLiteCommand (); order. Transaction = transaction; Command. Enter the commandtext = "TBL3 (ID, TBLIID) values (3, @ ORID)"; Command. Parameter. Add (new SQLTapMeter ("@ line IID", line ID)); Command.ExecuteNonQuery (); Transaction.Commit (); } Hold (Exception pre) {if (connection.State == ConnectionState.Open) transaction.Rollback (); MessageBox.Show (ex.Message); } Finally {connection.Close (); Transaction.Dispose (); Command.Dispose (); }
Comments
Post a Comment