public static void GrabarArticulosAgrupar(DataTable tblStock, DataTable tblArticulos, ref int? codigoError) { MySqlTransaction tr = null; try { MySqlConnection SqlConnection1 = DALBase.GetConnection(); tr = SqlConnection1.BeginTransaction(); DAL.ArticulosDAL.BorrarArticulosAgrupar(tblArticulos, SqlConnection1, tr); DAL.StockDAL.Update(tblStock, SqlConnection1, tr); tr.Commit(); SqlConnection1.Close(); } catch (MySqlException ex) { switch (ex.Number) { case 1042: //Unable to connect to any of the specified MySQL hosts. tblStock.RejectChanges(); tblArticulos.RejectChanges(); codigoError = 1042; break; case 0: // Procedure or function cannot be found in database tblStock.RejectChanges(); tblArticulos.RejectChanges(); codigoError = ex.Number; break; default: tblStock.RejectChanges(); tblArticulos.RejectChanges(); if (tr != null) { tr.Rollback(); } codigoError = ex.Number; break; } } catch (TimeoutException) { codigoError = 8953; // El número 8953 lo asigné al azar } catch (NullReferenceException) { codigoError = 8954; // El número 8954 lo asigné al azar } catch (Exception) { codigoError = 8955; // El número 8955 lo asigné al azar } }
public static void GrabarDB(DataTable tblUsuarios) { try { DAL.ColoresDAL.GrabarDB(tblUsuarios); } catch (MySqlException ex) { MessageBox.Show(ex.ToString(), "NcSoft", MessageBoxButtons.OK, MessageBoxIcon.Information); tblUsuarios.RejectChanges(); } }
public void setModified_testRollback() { DataTable table = new DataTable(); table.Columns.Add("col1", typeof(int)); table.Columns.Add("col2", typeof(int)); DataRow row = table.Rows.Add(new object[] { 1, 1 }); table.AcceptChanges(); row.SetModified (); Assert.AreEqual(row.RowState, DataRowState.Modified, "#0"); Assert.AreEqual(1, row [0, DataRowVersion.Current], "#1"); Assert.AreEqual(1, row [0, DataRowVersion.Original], "#2"); table.RejectChanges (); Assert.AreEqual(row.RowState, DataRowState.Unchanged, "#3"); }
public void setAdded_testRollback () { DataTable table = new DataTable (); table.Columns.Add ("col1", typeof (int)); table.Columns.Add ("col2", typeof (int)); table.Rows.Add (new object[] {1,1}); table.AcceptChanges (); table.Rows [0].SetAdded (); table.RejectChanges (); Assert.AreEqual (0, table.Rows.Count, "#1"); }
private int ActualizaBase(DataTable conjuntoDeDatos) { try { if (conjuntoDeDatos == null) return -1; // Comprobar errores DataRow[] RenglonesMal = conjuntoDeDatos.GetErrors(); // Si no hay errores se actualiza la base de // datos. En otro caso se avisa al usuario if (RenglonesMal.Length == 0) { int numeroDeRenglones = miAdaptador.Update(conjuntoDeDatos); conjuntoDeDatos.AcceptChanges(); Error = ""; misDatos = conjuntoDeDatos; return numeroDeRenglones; } else { Error = ""; foreach (DataRow renglon in RenglonesMal) { foreach (DataColumn columna in renglon.GetColumnsInError()) { Error += renglon.GetColumnError(columna) + "\n"; } } conjuntoDeDatos.RejectChanges(); misDatos = conjuntoDeDatos; return -1; } } catch (Exception ex) { Error = ex.Message; return -1; } }
public void Changes () //To test GetChanges and RejectChanges { DataTable table = new DataTable (); DataColumn col = new DataColumn (); col.ColumnName = "Id"; col.DataType = typeof (int); table.Columns.Add (col); UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] ); table.Constraints.Add (uc); col = new DataColumn (); col.ColumnName = "Name"; col.DataType = typeof (string); table.Columns.Add (col); DataRow row = table.NewRow (); row ["Id"] = 147; row ["name"] = "Abc"; table.Rows.Add (row); table.AcceptChanges (); row = table.NewRow (); row ["Id"] = 47; row ["name"] = "Efg"; table.Rows.Add (row); //Testing GetChanges DataTable changesTable = table.GetChanges (); Assert.AreEqual (1, changesTable.Rows.Count, "#A01"); Assert.AreEqual ("Efg", changesTable.Rows[0]["Name"], "#A02"); table.AcceptChanges (); changesTable = table.GetChanges (); try { int cnt = changesTable.Rows.Count; Assert.Fail (); } catch (NullReferenceException) { } //Testing RejectChanges row = table.NewRow (); row ["Id"] = 247; row ["name"] = "Hij"; table.Rows.Add (row); (table.Rows [0])["Name"] = "AaBbCc"; table.RejectChanges (); Assert.AreEqual ("Abc" , (table.Rows [0]) ["Name"], "#A03"); Assert.AreEqual (2, table.Rows.Count, "#A04"); }
private DataRow GetLockRow(DataTable tbl) { DataRow row = tbl.Rows.Find(m_Values); if (row == null) { try { DataRow newrow = tbl.NewRow(); newrow.BeginEdit(); newrow["tablename"] = m_strTableName; newrow["fieldnames"] = m_strKeys; newrow["fieldvalues"] = m_strValues; newrow["lastversion"] = 0; newrow["modid"] = DBNull.Value; newrow["lockip"] = DBNull.Value; newrow["lockhostname"] = DBNull.Value; newrow["lockusername"] = DBNull.Value; newrow["locktime"] = DBNull.Value;// DateTime.MinValue; newrow["unlocktime"] = DBNull.Value;//DateTime.MinValue; newrow.EndEdit(); tbl.Rows.Add(newrow); SqlCommandBuilder cmdBuilder = m_cmdBuilder;// new SqlCommandBuilder(m_adp); m_adp.InsertCommand = cmdBuilder.GetInsertCommand(); int val = m_adp.Update(tbl); tbl.AcceptChanges(); row = newrow; } catch (Exception ex) { tbl.RejectChanges(); throw ex; } } return row; }
public void LoadRowTestPreserveChanges () { DataTable dt = new DataTable (); dt.Columns.Add ("id", typeof (int)); dt.Columns.Add ("name", typeof (string)); dt.Rows.Add (new object [] { 1, "mono 1" }); dt.Rows.Add (new object [] { 2, "mono 2" }); dt.Rows.Add (new object [] { 3, "mono 3" }); dt.PrimaryKey = new DataColumn [] { dt.Columns ["id"] }; dt.AcceptChanges (); try { SubscribeEvents (dt); // current - modified; new - modified ResetEventFlags (); dt.LoadDataRow (new object [] { 2, "mono test" }, LoadOption.PreserveChanges); Assert.AreEqual (3, dt.Rows.Count, "#1 should not add a new row"); Assert.AreEqual ("mono test", dt.Rows [1] [1], "#2 should change the current"); Assert.AreEqual ("mono test", dt.Rows [1] [1, DataRowVersion.Original], "#3 should change the original"); Assert.AreEqual (DataRowState.Unchanged, dt.Rows [1].RowState, "#4 has not changed the row state"); Assert.IsTrue (rowChanging, "#ltpc11 row changing not called"); Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ltpc12 this row is not intended to change"); Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltpc13 row action is not Change"); Assert.IsTrue (rowChanged, "#ltpc14 row changed not called"); Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ltpc15 this row is not intended to change"); Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltpc16 row action is not Change"); // current - none; new - unchanged ResetEventFlags (); dt.LoadDataRow (new object [] { 4, "mono 4" }, LoadOption.PreserveChanges); Assert.AreEqual (4, dt.Rows.Count,"#5 should add a new row"); Assert.AreEqual ("mono 4", dt.Rows [3] [1], "#6 should change the current"); Assert.AreEqual ("mono 4", dt.Rows [3] [1, DataRowVersion.Original], "#7 should change the original"); Assert.AreEqual (DataRowState.Unchanged, dt.Rows [3].RowState, "#8 has not changed the row state"); Assert.IsTrue (rowChanging, "#ltpc21 row changing not called"); Assert.AreEqual (dt.Rows [3], rowInAction_Changing, "#ltpc22 this row is not intended to change"); Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changing, "#ltpc23 row action is not Change"); Assert.IsTrue (rowChanged, "#ltpc24 row changed not called"); Assert.AreEqual (dt.Rows [3], rowInAction_Changed, "#ltpc25 this row is not intended to change"); Assert.AreEqual (DataRowAction.ChangeCurrentAndOriginal, rowAction_Changed, "#ltpc16 row action is not Change"); dt.RejectChanges (); // current - added; new - modified dt.Rows.Add (new object [] { 5, "mono 5" }); ResetEventFlags (); dt.LoadDataRow (new object [] { 5, "mono test" }, LoadOption.PreserveChanges); Assert.AreEqual (5, dt.Rows.Count, "#9 should not add a new row"); Assert.AreEqual ("mono 5", dt.Rows [4] [1], "#10 should not change the current"); Assert.AreEqual ("mono test", dt.Rows [4] [1, DataRowVersion.Original], "#11 should change the original"); Assert.AreEqual (DataRowState.Modified, dt.Rows [4].RowState, "#12 has not changed the row state"); Assert.IsTrue (rowChanging, "#ltpc31 row changing not called"); Assert.AreEqual (dt.Rows [4], rowInAction_Changing, "#ltpc32 this row is not intended to change"); Assert.AreEqual (DataRowAction.ChangeOriginal, rowAction_Changing, "#ltpc33 row action is not Change"); Assert.IsTrue (rowChanged, "#ltpc34 row changed not called"); Assert.AreEqual (dt.Rows [4], rowInAction_Changed, "#ltpc35 this row is not intended to change"); Assert.AreEqual (DataRowAction.ChangeOriginal, rowAction_Changed, "#ltpc36 row action is not Change"); dt.RejectChanges (); // current - deleted ; new - deleted ChangeOriginal ResetEventFlags (); dt.Rows [1].Delete (); Assert.IsTrue (rowDeleting, "#ltpc37 row deleting"); Assert.IsTrue (rowDeleted, "#ltpc38 row deleted"); Assert.AreEqual (rowInAction_Deleting, dt.Rows[1], "#ltpc39 rowInAction_Deleting"); Assert.AreEqual (rowInAction_Deleted, dt.Rows[1], "#ltpc40 rowInAction_Deleted"); Assert.AreEqual (rowAction_Deleting, DataRowAction.Delete, "#ltpc60 rowInAction_Deleting"); Assert.AreEqual (rowAction_Deleted, DataRowAction.Delete, "#ltpc61 rowInAction_Deleted"); dt.LoadDataRow (new object [] { 2, "mono deleted" }, LoadOption.PreserveChanges); Assert.AreEqual (5, dt.Rows.Count, "#13 should not add a new row"); Assert.AreEqual ("mono deleted", dt.Rows [1] [1, DataRowVersion.Original], "#14 should change the original"); Assert.AreEqual (DataRowState.Deleted, dt.Rows [1].RowState, "#15 has not changed the row state"); Assert.IsTrue (rowChanging, "#ltpc41 row changing not called"); Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ltpc42 this row is not intended to change"); Assert.AreEqual (DataRowAction.ChangeOriginal, rowAction_Changing, "#ltoc43 row action is not Change"); Assert.IsTrue (rowChanged, "#ltpc44 row changed not called"); Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ltpc45 this row is not intended to change"); Assert.AreEqual (DataRowAction.ChangeOriginal, rowAction_Changed, "#ltpc46 row action is not Change"); } finally { UnsubscribeEvents (dt); } }
public void LoadRowTestUpsert () { DataTable dt = new DataTable (); dt.Columns.Add ("id", typeof (int)); dt.Columns.Add ("name", typeof (string)); dt.Rows.Add (new object [] { 1, "mono 1" }); dt.Rows.Add (new object [] { 2, "mono 2" }); dt.Rows.Add (new object [] { 3, "mono 3" }); dt.PrimaryKey = new DataColumn [] { dt.Columns ["id"] }; dt.AcceptChanges (); dt.LoadDataRow (new object [] { 2, "mono test" }, LoadOption.Upsert); Assert.AreEqual (3, dt.Rows.Count, "#1 should not add a row"); Assert.AreEqual ("mono test", dt.Rows [1] [1], "#2 should change the current"); Assert.AreEqual ("mono 2", dt.Rows [1] [1, DataRowVersion.Original], "#3 should not change original"); Assert.AreEqual (DataRowState.Modified, dt.Rows [1].RowState, "#4 should change state"); // Row State tests // current - modified ; result - modified dt.LoadDataRow (new object [] { 2, "mono test 2" }, LoadOption.Upsert); Assert.AreEqual ("mono test 2", dt.Rows [1] [1], "#c1 should change the current"); Assert.AreEqual ("mono 2", dt.Rows [1] [1, DataRowVersion.Original], "#c2 should not change original"); Assert.AreEqual (DataRowState.Modified, dt.Rows [1].RowState, "#c3 should not change state"); // current - Unchanged; result - Unchanged if no new value dt.AcceptChanges (); dt.LoadDataRow (new object [] { 2, "mono test 2" }, LoadOption.Upsert); Assert.AreEqual ("mono test 2", dt.Rows [1] [1], "#c4 should change the current"); Assert.AreEqual ("mono test 2", dt.Rows [1] [1, DataRowVersion.Original], "#c5 should not change original"); Assert.AreEqual (DataRowState.Unchanged, dt.Rows [1].RowState, "#c6 should not change state"); // not the same value again dt.RejectChanges (); dt.LoadDataRow (new object [] { 2, "mono test 3" }, LoadOption.Upsert); Assert.AreEqual (DataRowState.Modified, dt.Rows [1].RowState, "#c7 should not change state"); // current - added; result - added dt.Rows.Add (new object [] { 4, "mono 4" }); dt.LoadDataRow (new object [] { 4, "mono 4" }, LoadOption.Upsert); Assert.AreEqual ("mono 4", dt.Rows [3] [1], "#c8 should change the current"); try { object o = dt.Rows [3] [1, DataRowVersion.Original]; Assert.Fail ("#c9 should have thrown version not found exception"); } catch (VersionNotFoundException) { } Assert.AreEqual (DataRowState.Added, dt.Rows [3].RowState, "#c10 should not change state"); // current - new; result - added dt.LoadDataRow (new object [] { 5, "mono 5" }, LoadOption.Upsert); Assert.AreEqual ("mono 5", dt.Rows [4] [1], "#c11 should change the current"); try { object o = dt.Rows [4] [1, DataRowVersion.Original]; Assert.Fail ("#c12 should have thrown version not found exception"); } catch (VersionNotFoundException) { } Assert.AreEqual (DataRowState.Added, dt.Rows [4].RowState, "#c13 should change state"); // current - deleted; result - added a new row dt.AcceptChanges (); dt.Rows [4].Delete (); dt.LoadDataRow (new object [] { 5, "mono 5" }, LoadOption.Upsert); Assert.AreEqual (6, dt.Rows.Count, "#c14 should not add a row"); Assert.AreEqual ("mono 5", dt.Rows [5] [1], "#c15 should change the current"); try { object o = dt.Rows [5] [1, DataRowVersion.Original]; Assert.Fail ("#c16 expected version not found exception "); } catch (VersionNotFoundException) {} Assert.AreEqual (DataRowState.Added, dt.Rows [5].RowState, "#c17 should change state"); }
private bool UpdateRow(DataTable tbl, DataRow row, int nLastVer, DateTime dtLock, DateTime dtUnLock, bool bLock) { try { row.BeginEdit(); row["tablename"] = m_strTableName; row["lastversion"] = nLastVer; if (bLock) { row["lockip"] = m_strIP; row["lockhostname"] = m_strHostName; row["lockusername"] = m_strHostName; if (dtLock == DateTime.MinValue) row["locktime"] = DBNull.Value; else row["locktime"] = dtLock; if (dtUnLock == DateTime.MinValue) row["unlocktime"] = DBNull.Value; else row["unlocktime"] = dtUnLock; } else { row["lockip"] = DBNull.Value; row["lockhostname"] = DBNull.Value; row["lockusername"] = DBNull.Value; row["locktime"] = DBNull.Value; if (dtUnLock == DateTime.MinValue) row["unlocktime"] = DBNull.Value; else row["unlocktime"] = dtUnLock; } row.EndEdit(); SqlCommandBuilder cmdBuilder = m_cmdBuilder;// new SqlCommandBuilder(m_adp); m_adp.UpdateCommand = cmdBuilder.GetUpdateCommand(); int val = m_adp.Update(tbl); tbl.AcceptChanges(); return val > 0; } catch (Exception ex) { tbl.RejectChanges(); MessageBox.Show(ex.Message); throw ex; } }
/// <summary> /// private Methode zum Update der Datenbankdatei! /// </summary> /// <param name="adapter"></param> /// <param name="tabelle"></param> private void _Update(OleDbDataAdapter adapter, DataTable tabelle) { try { // Änderungen im Spiegel für die Festplattendatenbank übernehmen adapter.Update(tabelle); // Änderungen als übernommen markieren tabelle.AcceptChanges(); } catch (Exception) { // Sollte ein Fehler auftreten, werden die Änderungen im Spiegel verworfen. tabelle.RejectChanges(); throw new Exception("Änderungen an der Datenbank konnte nicht durchgeführt werden!"); } }
private bool UpdateRow(DataTable tbl, DataRow row, int nLastVer, DateTime dtLock, DateTime dtUnLock, bool bLock) { int val = 0; try { if (bLock) { row.BeginEdit(); row["fieldid"] = m_nFieldID; row["modtabID"] = m_nModTabID; row["tablename"] = m_strTableName; row["fieldname"] = m_strFieldName; row["lastversion"] = nLastVer; row["lockip"] = m_strIP; row["lockhostname"] = m_strHostName; row["lockusername"] = m_strHostName; if (dtLock == DateTime.MinValue) row["locktime"] = DBNull.Value; else row["locktime"] = dtLock; if (dtUnLock == DateTime.MinValue) row["unlocktime"] = DBNull.Value; else row["unlocktime"] = dtUnLock; row.EndEdit(); } else { row.Delete(); } SqlCommandBuilder cmdBuilder = m_cmdBuilder;// new SqlCommandBuilder(m_adp); m_adp.UpdateCommand = cmdBuilder.GetUpdateCommand(); val = m_adp.Update(tbl); tbl.AcceptChanges(); return val > 0; } catch (Exception ex) { tbl.RejectChanges(); throw ex; } }
private void btn_Undo_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.RejectChanges(); }
/// <summary> /// 撤消网格中的更改 /// </summary> /// <param name="dgv">DataGridView 控件</param> /// <param name="dt">DataGridView的DataSource的DataTable</param> public static void RejectChanges(DataGridView dgv, SD.DataTable dt) { dt.RejectChanges(); dgv.Refresh(); }
public void RejectChanges_CheckIndex () { DataTable table = new DataTable (); table.Columns.Add ("col1", typeof (int)); table.PrimaryKey = new DataColumn[] {table.Columns [0]}; table.Rows.Add (new object[] {1}); table.AcceptChanges (); table.Rows [0][0] = 10; table.RejectChanges (); Assert.IsNotNull (table.Rows.Find (1)); }
/// <summary> /// 条件に当てはまるレコードをDataTableから削除します。 /// </summary> /// <param name="dt">データテーブル</param> /// <param name="filter">条件</param> /// <returns>0:正常終了 -1:異常終了</returns> public static int RemoveSelectRows(DataTable dt, string filter) { try { DataRow[] rows = dt.Select(filter); for (int i = 0; i < rows.Length; i++) { if (rows[i].RowState != DataRowState.Deleted) { rows[i].Delete(); } } dt.AcceptChanges(); return 0; } catch (Exception) { dt.RejectChanges(); return -1; } }
public int UpdateDataBase(DbDataAdapter da, DataTable dt) { int updateCount = 0; try { DataTable dtChanges = dt.GetChanges(); if (dtChanges != null && dtChanges.Rows.Count >= 0) { updateCount = da.Update(dtChanges); dt.AcceptChanges(); } } catch (Exception) { dt.RejectChanges(); throw; } return updateCount; }
public void Changes () //To test GetChanges and RejectChanges { DataTable table = new DataTable (); DataColumn col = new DataColumn (); col.ColumnName = "Id"; col.DataType = Type.GetType ("System.Int32"); table.Columns.Add (col); UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] ); table.Constraints.Add (uc); col = new DataColumn (); col.ColumnName = "Name"; col.DataType = Type.GetType ("System.String"); table.Columns.Add (col); DataRow row = table.NewRow (); row ["Id"] = 147; row ["name"] = "Abc"; table.Rows.Add (row); table.AcceptChanges (); row = table.NewRow (); row ["Id"] = 47; row ["name"] = "Efg"; table.Rows.Add (row); //Testing GetChanges DataTable changesTable = table.GetChanges (); AssertEquals ("#A01", 1 ,changesTable.Rows.Count); AssertEquals ("#A02","Efg" ,changesTable.Rows[0]["Name"]); table.AcceptChanges (); changesTable = table.GetChanges (); try { int cnt = changesTable.Rows.Count; } catch(Exception e) { if (e.GetType () != typeof (AssertionException)) AssertEquals ("#A03",typeof(NullReferenceException) ,e.GetType ()); else Console.WriteLine (e); } //Testing RejectChanges row = table.NewRow (); row ["Id"] = 247; row ["name"] = "Hij"; table.Rows.Add (row); (table.Rows [0])["Name"] = "AaBbCc"; table.RejectChanges (); AssertEquals ("#A03", "Abc" , (table.Rows [0]) ["Name"]); AssertEquals ("#A04", 2, table.Rows.Count); }
public void LoadRowTestUpsert () { DataTable dt = new DataTable (); dt.Columns.Add ("id", typeof (int)); dt.Columns.Add ("name", typeof (string)); dt.Rows.Add (new object [] { 1, "mono 1" }); dt.Rows.Add (new object [] { 2, "mono 2" }); dt.Rows.Add (new object [] { 3, "mono 3" }); dt.PrimaryKey = new DataColumn [] { dt.Columns ["id"] }; dt.AcceptChanges (); try { SubscribeEvents (dt); ResetEventFlags (); dt.LoadDataRow (new object [] { 2, "mono test" }, LoadOption.Upsert); Assert.AreEqual (3, dt.Rows.Count, "#1 should not add a row"); Assert.AreEqual ("mono test", dt.Rows [1] [1], "#2 should change the current"); Assert.AreEqual ("mono 2", dt.Rows [1] [1, DataRowVersion.Original], "#3 should not change original"); Assert.AreEqual (DataRowState.Modified, dt.Rows [1].RowState, "#4 should change state"); Assert.IsTrue (rowChanging, "#ev1 row changing not called"); Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ev2 this row is not intended to change"); Assert.AreEqual (DataRowAction.Change, rowAction_Changing, "#ev3 row action is not Change"); Assert.IsTrue (rowChanged, "#ev4 row changed not called"); Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ev5 this row is not intended to change"); Assert.AreEqual (DataRowAction.Change, rowAction_Changed, "#ev6 row action is not Change"); // Row State tests // current - modified ; result - modified ResetEventFlags (); dt.LoadDataRow (new object [] { 2, "mono test 2" }, LoadOption.Upsert); Assert.AreEqual ("mono test 2", dt.Rows [1] [1], "#c1 should change the current"); Assert.AreEqual ("mono 2", dt.Rows [1] [1, DataRowVersion.Original], "#c2 should not change original"); Assert.AreEqual (DataRowState.Modified, dt.Rows [1].RowState, "#c3 should not change state"); Assert.IsTrue (rowChanging, "#ev11 row changing not called"); Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ev12 this row is not intended to change"); Assert.AreEqual (DataRowAction.Change, rowAction_Changing, "#ev13 row action is not Change"); Assert.IsTrue (rowChanged, "#ev14 row changed not called"); Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ev15 this row is not intended to change"); Assert.AreEqual (DataRowAction.Change, rowAction_Changed, "#ev16 row action is not Change"); // current - Unchanged; result - Unchanged if no new value dt.AcceptChanges (); ResetEventFlags (); dt.LoadDataRow (new object [] { 2, "mono test 2" }, LoadOption.Upsert); Assert.AreEqual ("mono test 2", dt.Rows [1] [1], "#c4 should not change the current"); Assert.AreEqual ("mono test 2", dt.Rows [1] [1, DataRowVersion.Original], "#c5 should not change original"); Assert.AreEqual (DataRowState.Unchanged, dt.Rows [1].RowState, "#c6 should not change state"); Assert.IsTrue (rowChanging, "#ev21 row changing not called"); Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ev22 this row is not intended to change"); Assert.AreEqual (DataRowAction.Nothing, rowAction_Changing, "#ev13 row action is not Nothing"); Assert.IsTrue (rowChanged, "#ev24 row changed not called"); Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ev25 this row is not intended to change"); Assert.AreEqual (DataRowAction.Nothing, rowAction_Changed, "#ev26 row action is not Nothing"); // not the same value again dt.RejectChanges (); ResetEventFlags (); dt.LoadDataRow (new object [] { 2, "mono test 3" }, LoadOption.Upsert); Assert.AreEqual (DataRowState.Modified, dt.Rows [1].RowState, "#c7 should not change state"); Assert.IsTrue (rowChanging, "#ev31 row changing not called"); Assert.AreEqual (dt.Rows [1], rowInAction_Changing, "#ev32 this row is not intended to change"); Assert.AreEqual (DataRowAction.Change, rowAction_Changing, "#ev33 row action is not Change"); Assert.IsTrue (rowChanged, "#ev34 row changed not called"); Assert.AreEqual (dt.Rows [1], rowInAction_Changed, "#ev35 this row is not intended to change"); Assert.AreEqual (DataRowAction.Change, rowAction_Changed, "#ev36 row action is not Change"); // current - added; result - added dt.Rows.Add (new object [] { 4, "mono 4" }); ResetEventFlags (); dt.LoadDataRow (new object [] { 4, "mono 4" }, LoadOption.Upsert); Assert.AreEqual ("mono 4", dt.Rows [3] [1], "#c8 should change the current"); try { object o = dt.Rows [3] [1, DataRowVersion.Original]; Assert.Fail ("#c9 should have thrown version not found exception"); } catch (VersionNotFoundException) { } Assert.AreEqual (DataRowState.Added, dt.Rows [3].RowState, "#c10 should not change state"); Assert.IsTrue (rowChanging, "#ev41 row changing not called"); Assert.AreEqual (dt.Rows [3], rowInAction_Changing, "#ev42 this row is not intended to change"); Assert.AreEqual (DataRowAction.Change, rowAction_Changing, "#ev43 row action is not Change"); Assert.IsTrue (rowChanged, "#ev44 row changed not called"); Assert.AreEqual (dt.Rows [3], rowInAction_Changed, "#ev45 this row is not intended to change"); Assert.AreEqual (DataRowAction.Change, rowAction_Changed, "#ev46 row action is not Change"); // current - none; result - added ResetEventFlags (); dt.LoadDataRow (new object [] { 5, "mono 5" }, LoadOption.Upsert); Assert.AreEqual ("mono 5", dt.Rows [4] [1], "#c11 should change the current"); try { object o = dt.Rows [4] [1, DataRowVersion.Original]; Assert.Fail ("#c12 should have thrown version not found exception"); } catch (VersionNotFoundException) { } Assert.AreEqual (DataRowState.Added, dt.Rows [4].RowState, "#c13 should change state"); Assert.IsTrue (rowChanging, "#ev51 row changing not called"); Assert.AreEqual (dt.Rows [4], rowInAction_Changing, "#ev52 this row is not intended to change"); Assert.AreEqual (DataRowAction.Add, rowAction_Changing, "#ev53 row action is not Change"); Assert.IsTrue (rowChanged, "#ev54 row changed not called"); Assert.AreEqual (dt.Rows [4], rowInAction_Changed, "#ev55 this row is not intended to change"); Assert.AreEqual (DataRowAction.Add, rowAction_Changed, "#ev56 row action is not Change"); // current - deleted; result - added a new row ResetEventFlags (); dt.AcceptChanges (); dt.Rows [4].Delete (); Assert.IsTrue (rowDeleting, "#ev57 row deleting"); Assert.IsTrue (rowDeleted, "#ev58 row deleted"); Assert.AreEqual (rowInAction_Deleting, dt.Rows[4], "#ev59 rowInAction_Deleting"); Assert.AreEqual (rowInAction_Deleted, dt.Rows[4], "#ev59 rowInAction_Deleted"); Assert.AreEqual (rowAction_Deleting, DataRowAction.Delete, "#ev60 rowInAction_Deleting"); Assert.AreEqual (rowAction_Deleted, DataRowAction.Delete, "#ev61 rowInAction_Deleted"); dt.LoadDataRow (new object [] { 5, "mono 5" }, LoadOption.Upsert); Assert.AreEqual (6, dt.Rows.Count, "#c14 should not add a row"); Assert.AreEqual ("mono 5", dt.Rows [5] [1], "#c15 should change the current"); try { object o = dt.Rows [5] [1, DataRowVersion.Original]; Assert.Fail ("#c16 expected version not found exception "); } catch (VersionNotFoundException) { } Assert.AreEqual (DataRowState.Added, dt.Rows [5].RowState, "#c17 should change state"); Assert.IsTrue (rowChanging, "#ev61 row changing not called"); Assert.AreEqual (dt.Rows [5], rowInAction_Changing, "#ev62 this row is not intended to change"); Assert.AreEqual (DataRowAction.Add, rowAction_Changing, "#ev63 row action is not Change"); Assert.IsTrue (rowChanged, "#ev64 row changed not called"); Assert.AreEqual (dt.Rows [5], rowInAction_Changed, "#ev65 this row is not intended to change"); Assert.AreEqual (DataRowAction.Add, rowAction_Changed, "#ev66 row action is not Change"); } finally { UnsubscribeEvents (dt); } }
public bool SaveDataTable(DataTable dtTable, string[] tableName) { if (dtTable == null) return false; if (dtTable.Rows.Count <= 0) return false; bool isDuplicate = false; DataRow drOld = null; DataRow dr = null; DataTable dtSave = dtTable.Copy(); dtTable.RejectChanges(); try { if ((dtSave != null) && dtSave.Rows.Count > 0) { //foreach(DataRow dr in dtSave.Rows) for (int i = 0; i < dtSave.Rows.Count; i++) { foreach (string tblN in tableName) { dr = dtSave.Rows[i]; if (IsEmptyRow(dr, tblN)) continue; if (dr.RowState == DataRowState.Added) { //Check duplicate isDuplicate = IsDupplication(dr, dtSave, tblN); if (isDuplicate) { return !isDuplicate; } //Insert new record MakeInsertNewQuery(dr, dtSave, tblN); EZLog(dr, dr, tblN); } else if (dr.RowState == DataRowState.Modified) { // Update existing record MakeUpdateQuery(dr, tblN); if (!isNoUpdate) { drOld = dtTable.Rows[i]; EZLog(drOld, dr, tblN); } } } } } if (!isDuplicate) { dtSave.AcceptChanges(); HPA.Common.Methods.ShowMessage(Common.CommonConst.DATASAVED_SUCCESSFULLY, MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { // dtSave.RejectChanges(); throw (ex); } return true; }
private bool UpdateRow(DataTable tbl, DataRow row, int nLastVer, DateTime dtLock, DateTime dtUnLock, int nModId, bool bLock) { int val = 0; try { row.BeginEdit(); row["tablename"] = m_strTableName; row["fieldnames"] = m_strKeys; row["fieldvalues"] = m_strValues; row["lastversion"] = nLastVer; if (bLock) { row["lockip"] = m_strIP; row["lockhostname"] = m_strHostName; row["lockusername"] = m_strHostName; if (dtLock == DateTime.MinValue) row["locktime"] = DBNull.Value; else row["locktime"] = dtLock; if (dtUnLock == DateTime.MinValue) row["unlocktime"] = DBNull.Value; else row["unlocktime"] = dtUnLock; if (nModId == -1) row["modid"] = DBNull.Value; else row["modid"] = nModId; } else { row["lockip"] = DBNull.Value; row["lockhostname"] = DBNull.Value; row["lockusername"] = DBNull.Value; row["locktime"] = DBNull.Value; if (dtUnLock == DateTime.MinValue) row["unlocktime"] = DBNull.Value; else row["unlocktime"] = dtUnLock; row["modid"] = DBNull.Value; } row.EndEdit(); SqlCommandBuilder cmdBuilder = m_cmdBuilder;// new SqlCommandBuilder(m_adp); m_adp.UpdateCommand = cmdBuilder.GetUpdateCommand(); val = m_adp.Update(tbl); tbl.AcceptChanges(); return val > 0; } catch (Exception ex) { if (val == 0) { string[] keynames = new string[3]; keynames[0] = "tablename"; keynames[1] = "fieldnames"; keynames[2] = "fieldvalues"; string[] keys = new string[3]; keys[0] = m_strTableName; keys[1] = m_strKeys; keys[2] = m_strValues; string[] fields = new string[7]; fields[0] = "lastversion"; fields[1] = "lockip"; fields[2] = "lockhostname"; fields[3] = "lockusername"; fields[4] = "locktime"; fields[5] = "unlocktime"; fields[6] = "modid"; string[] values = new string[7]; values[0] = nLastVer.ToString(); values[1] = m_strIP; values[2] = m_strHostName; values[3] = m_strHostName; values[4] = row["locktime"] == DBNull.Value ? "<NULL>" : row["locktime"].ToString().Trim(); values[5] = row["unlocktime"] == DBNull.Value ? "<NULL>" : row["unlocktime"].ToString().Trim(); values[6] = row["modid"].ToString().Trim(); return SqlUpdateRecord("sys_lock_record", keynames, keys, fields, values, m_conn); } else { tbl.RejectChanges(); throw ex; } } }
/// <summary> /// 基本表單更新BCO /// </summary> /// <param name="ParameterList">輸入變數</param> /// <param name="RootDBT">是否有主交易,無主交易輸入null</param> /// <returns>回傳更新成功與否</returns> public bool UpdateMaster(ArrayList ParameterList, DataTable newDetail, DbTransaction RootDBT, DataTable oldDetail, DataTable dtDetail, DateTime d_Now) { #region bool IsRootTranscation = false; bool bResult = false; try { //判斷是否有傳入Root Transcation IsRootTranscation = (RootDBT == null) ? true : false; #region 啟動交易或指定RootTranscation if (IsRootTranscation) { //獨立呼叫啟動Transcation Conn = USEDB.CreateConnection(); Conn.Open(); DBT = Conn.BeginTransaction(); } else { DBT = RootDBT; } #endregion #region 通路管制設定明細修改 if (dtDetail == null || (dtDetail != null && dtDetail.Rows.Count <= 0)) { bResult = true; } else { foreach (DataRow dRow in dtDetail.Rows) { bResult = UpdateChannel(ParameterList, dRow, DBT, d_Now); dRow["UPDATEDATE"] = d_Now; dRow["UPDATEUID"] = ParameterList[2].ToString(); } } #endregion #region 在oldDetail逐筆檢查資料,判斷是否存在newDetail中,如無則刪除的項目 foreach (DataRow dRow in oldDetail.Rows) { try { string s_FilterExpression = string.Empty; s_FilterExpression = "ITEM ='" + dRow["ITEM"].ToString() + "' AND PERIOD ='" + dRow["PERIOD"].ToString() + "' AND VIRTUAL_CODE ='" + dRow["VIRTUAL_CODE"].ToString() + "'"; DataRow dRow_Oringinal = newDetail.Select(s_FilterExpression)[0]; } catch (Exception ex) { #region 在新來源找不到此筆資料,所以要呼叫 delete 明細 DeleteDetail(ParameterList, dRow, DBT); #endregion } } #endregion #region 在newDetail逐筆檢查資料,判斷是否有新增、修改的項目 foreach (DataRow dRow in newDetail.Rows) { try { string s_FilterExpression = string.Empty; s_FilterExpression = "ITEM ='" + dRow["ITEM"].ToString() + "' AND PERIOD ='" + dRow["PERIOD"].ToString() + "' AND VIRTUAL_CODE ='" + dRow["VIRTUAL_CODE"].ToString() + "'"; DataRow dRow_Oringinal = oldDetail.Select(s_FilterExpression)[0]; //當理貨/管制不一致時,才需呼叫update if (dRow["PICK_SELECT"].ToString() != dRow_Oringinal["PICK_SELECT"].ToString()) { bResult = UpdateDetail(ParameterList, dRow, dRow_Oringinal, DBT); } } catch (Exception ex) { #region 在舊來源找不到此筆資料,所以要呼叫 insert 明細 DataRow[] DeTail; if (dtDetail != null && dtDetail.Rows.Count > 0) { DeTail = dtDetail.Select("ITEM ='" + dRow["ITEM"].ToString() + "' AND PERIOD ='" + dRow["PERIOD"].ToString() + "' AND VIRTUAL_CODE ='" + dRow["VIRTUAL_CODE"].ToString() + "'"); if (DeTail != null) if (DeTail.Length > 0) CreateDetail(ParameterList, dRow, DBT, d_Now, "1"); else CreateDetail(ParameterList, dRow, DBT, d_Now, "0"); else CreateDetail(ParameterList, dRow, DBT, d_Now, "0"); } else CreateDetail(ParameterList, dRow, DBT, d_Now, "0"); #endregion } } #endregion #region 交易成功 if (IsRootTranscation) { if (dtDetail != null) dtDetail.AcceptChanges(); //獨立呼叫Transcation成立 DBT.Commit(); } #endregion return bResult; } catch (Exception ex) { #region 交易失敗 if (IsRootTranscation) { //獨立呼叫Transcation失敗 DBT.Rollback(); } if (dtDetail != null) dtDetail.RejectChanges(); #endregion throw ex; } finally { #region 判斷是否關閉交易連線 if (IsRootTranscation) { //獨立呼叫Transcation,關閉連線 if (Conn.State == ConnectionState.Connecting) { Conn.Close(); } } #endregion } #endregion }
public void ImportRowDeletedTest () { DataTable table = new DataTable (); table.Columns.Add ("col", typeof (int)); table.Columns.Add ("col1", typeof (int)); DataRow row = table.Rows.Add (new object[] {1,2}); table.PrimaryKey = new DataColumn[] {table.Columns[0]}; table.AcceptChanges (); // If row is in Deleted state, then ImportRow loads the // row. row.Delete (); table.ImportRow (row); Assert.AreEqual (2, table.Rows.Count, "#A1"); // Both the deleted rows shud be now gone table.AcceptChanges (); Assert.AreEqual (0, table.Rows.Count, "#A2"); //just add another row row = table.Rows.Add (new object[] {1,2}); // no exception shud be thrown table.AcceptChanges (); // If row is in Deleted state, then ImportRow loads the // row and validate only on RejectChanges row.Delete (); table.ImportRow (row); Assert.AreEqual (2, table.Rows.Count, "#A3"); Assert.AreEqual (DataRowState.Deleted, table.Rows[1].RowState, "#A4"); try { table.RejectChanges (); Assert.Fail ("#B1"); } catch (ConstraintException ex) { // Column 'col' is constrained to be unique. // Value '1' is already present Assert.AreEqual (typeof (ConstraintException), ex.GetType (), "#B2"); Assert.IsNull (ex.InnerException, "#B3"); Assert.IsNotNull (ex.Message, "#B4"); Assert.IsTrue (ex.Message.IndexOf ("'col'") != -1, "#B5"); Assert.IsTrue (ex.Message.IndexOf ("'1'") != -1, "#B6"); } }
public void FindByKey () { DataTable table = new DataTable (); table.Columns.Add ("col1", typeof (int)); table.PrimaryKey = new DataColumn[] {table.Columns [0]}; table.Rows.Add (new object[] {1}); table.Rows.Add (new object[] {2}); table.Rows.Add (new object[] {3}); table.AcceptChanges (); Assert.IsNotNull (table.Rows.Find (new object[] {1}), "#1"); table.Rows[0].Delete (); Assert.IsNull (table.Rows.Find (new object[] {1}), "#2"); table.RejectChanges (); Assert.IsNotNull (table.Rows.Find (new object[] {1}), "#3"); }