database - How to Add a command/SQL statement to a strongly typed TableAdapter's Update/Insert command? -


I have the following code that executes against a SQLIte database using a drastic typed dataset.

  messagesAdapter.Update (messages); // message a datatable var connection = messageadapter.cnation; Var Recovery Index Commands = connection.CreateCommand (); RetrieveIndexCommand.CommandText = "Choose Last_insert_rowid ()"; Connection.Open (); Var index = retrieveIndexCommand.ExecuteScalar (); Connection.Close ();  

This does not work as the Last_Innsor_Orid () always returns zero. This is due to the fact that it should be called during the same connection which is used by the update command of TableAdapter. How do I insert TableAdapter or update the update so that it returns the pointer?

If you insert a line, you can use it:

  // if necessary (using var insert = (SQLiteCommand) Adapter.InsertCommand.Clone ()) {insert.CommandText + = "; SELECT last_insert_rowid ()"; Foreach (SQLiteParameter parameter in insert.Parameters) {parameter.Value = Line [parameter. SourceColumn]; }} Var index = Convert.ToInt32 (insert.ExecuteScalar ());  

You can also use it to insert multiple rows and assign your ID for each:

 using  ( Var insert = (SQLiteCommand) this.Adapter InsertCommand.Clone ()) {insert.CommandText + = "; SELECT last_insert_rowid ()"; // This filter has been added only to the foreach (MyDataSet.MessageRow line in the message. GetChanges (DataRowState.Added)) {foreach (SQLiteParameter parameter in insert.Parameters) {parameter.Value = row [parameter.SourceColumn]; } // Use the name of your lined column line. Id = convert ToInt32 (insert.ExecuteScalar ()); Row.AcceptChanges (); }} // Then you have other Updates Message Adapter. Touch the update (message);  

Note: Be sure to open / close your connection


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -