/// <summary> /// This method is called every time a row is updated and stores the insert id into the DataTable. /// </summary> private void DataAdapter_RowUpdated(object sender, System.Data.Common.RowUpdatedEventArgs e) { if (e.Status == UpdateStatus.Continue && e.StatementType == StatementType.Insert) { e.Row["id"] = ((SQLiteConnection)e.Command.Connection).LastInsertRowId; e.Row.AcceptChanges(); } }
private void _dbDataAdapter_RowUpdated(object sender, System.Data.Common.RowUpdatedEventArgs e) { //https://msdn.microsoft.com/en-us/library/ks9f57t0%28v=vs.110%29.aspx if (e.StatementType == StatementType.Insert) { var getIdCommand = new SQLiteCommand("SELECT last_insert_rowid()", _dbConnection); e.Row["Id"] = (long)getIdCommand.ExecuteScalar(); } }
// Save is only posible if ID is known void m_dataAdapter_RowUpdated(object sender, System.Data.Common.RowUpdatedEventArgs e) { // The (just receaved?) ID is only interesting with a new record if (e.StatementType == StatementType.Insert) { // Determin the just receaved ID SQLiteCommand command = new SQLiteCommand ("SELECT last_insert_rowid() AS ID", m_connection); // Get the new ID and Save in the according field object newID = command.ExecuteScalar(); // BIf errors then no ID --> thus testing required if (newID == System.DBNull.Value == false) { // Put the ID in the DataRow } } }
// Rij is opgeslagen, bepaal eventueel de nieuwe ID void m_dataAdapter_RowUpdated(object sender, System.Data.Common.RowUpdatedEventArgs e) { // Het (zojuist verkregen?) ID is alleen interessant bij een nieuwe record if (e.StatementType == StatementType.Insert) { // Bepaal het zojuist verkregen ID //SQLiteCommand command = new SQLiteCommand("SELECT @@IDENTITY", m_connection); SQLiteCommand command = new SQLiteCommand("SELECT last_insert_rowid() AS ID", m_connection); // Bepaal de nieuwe ID en sla deze op in het juiste veld object nieuweID = command.ExecuteScalar(); // Bij evt. fouten geen ID --> Daarom testen if (nieuweID == System.DBNull.Value == false) { // Zet de ID in de juiste kolom in de DataRow e.Row[m_fieldNameID] = Convert.ToInt32(nieuweID); } } }
protected override void OnRowUpdated(System.Data.Common.RowUpdatedEventArgs value) { }
// If it's an Insert we fetch the @@Identity value and stuff it in the proper column protected static void OnRowUpdated(object sender, System.Data.Common.RowUpdatedEventArgs e) { try { PropertyCollection props = e.Row.Table.ExtendedProperties; if (props.ContainsKey("props")) { props = (PropertyCollection)props["props"]; } if (e.Status == UpdateStatus.Continue && (e.StatementType == StatementType.Insert || e.StatementType == StatementType.Update)) { esDataRequest request = props["esDataRequest"] as esDataRequest; esEntitySavePacket packet = (esEntitySavePacket)props["esEntityData"]; if (e.StatementType == StatementType.Insert) { if (props.Contains("AutoInc")) { string autoInc = props["AutoInc"] as string; SQLiteCommand cmd = new SQLiteCommand(); cmd.Connection = e.Command.Connection as SQLiteConnection; cmd.Transaction = e.Command.Transaction as SQLiteTransaction; cmd.CommandText = "SELECT last_insert_rowid()"; cmd.CommandType = CommandType.Text; object o = null; o = cmd.ExecuteScalar(); if (o != null) { packet.CurrentValues[autoInc] = o; e.Row[autoInc] = o; } } if (props.Contains("EntitySpacesConcurrency")) { string esConcurrencyColumn = props["EntitySpacesConcurrency"] as string; packet.CurrentValues[esConcurrencyColumn] = 1; } } //------------------------------------------------------------------------------------------------- // Fetch any defaults, SQLite doesn't support output parameters so we gotta do this the hard way //------------------------------------------------------------------------------------------------- if (props.Contains("Defaults")) { // Build the Where parameter and parameters SQLiteCommand cmd = new SQLiteCommand(); cmd.Connection = e.Command.Connection as SQLiteConnection; cmd.Transaction = e.Command.Transaction as SQLiteTransaction; string select = (string)props["Defaults"]; string[] whereParameters = ((string)props["Where"]).Split(','); string comma = String.Empty; string where = String.Empty; int i = 1; foreach (string parameter in whereParameters) { SQLiteParameter p = new SQLiteParameter("@p" + i++.ToString(), e.Row[parameter]); cmd.Parameters.Add(p); where += comma + "[" + parameter + "]=" + p.ParameterName; comma = " AND "; } // Okay, now we can execute the sql and get any values that have defaults that were // null at the time of the insert and/or our timestamp cmd.CommandText = "SELECT " + select + " FROM [" + request.ProviderMetadata.Source + "] WHERE " + where + ";"; SQLiteDataReader rdr = null; try { rdr = cmd.ExecuteReader(CommandBehavior.SingleResult); if (rdr.Read()) { select = select.Replace("[", String.Empty).Replace("]", String.Empty); string[] selectCols = select.Split(','); for (int k = 0; k < selectCols.Length; k++) { packet.CurrentValues[selectCols[k]] = rdr.GetValue(k); } } } finally { // Make sure we close the reader no matter what if (rdr != null) { rdr.Close(); } } } if (e.StatementType == StatementType.Update) { string colName = props["EntitySpacesConcurrency"] as string; object o = e.Row[colName]; switch (Type.GetTypeCode(o.GetType())) { case TypeCode.Int16: packet.CurrentValues[colName] = ((System.Int16)o) + 1; break; case TypeCode.Int32: packet.CurrentValues[colName] = ((System.Int32)o) + 1; break; case TypeCode.Int64: packet.CurrentValues[colName] = ((System.Int64)o) + 1; break; case TypeCode.UInt16: packet.CurrentValues[colName] = ((System.UInt16)o) + 1; break; case TypeCode.UInt32: packet.CurrentValues[colName] = ((System.UInt32)o) + 1; break; case TypeCode.UInt64: packet.CurrentValues[colName] = ((System.UInt64)o) + 1; break; } } } } catch { } }