Tuesday, 10 April 2012

Using transactions with entity data module

  DatabaseModel.DatabaseEntities o = new DatabaseEntities();
        o.Connection.Open();
        using (System.Data.Common.DbTransaction dbt = o.Connection.BeginTransaction())
        {
            try
            {
                EMP6 p = new EMP6();
                p.EID = Convert.ToInt32(tb.Text);
                p.NAME = tb1.Text;
                o.EMP6.AddObject(p);
                o.SaveChanges();

                for (i = 0; i < gv.Rows.Count; i++)
                {
                    EMP7 q = new EMP7();
                    q.EID = Convert.ToInt32(tb.Text);
                    q.DID = Convert.ToInt32(gv.Rows[i].Cells[0].Text);
                    o.EMP7.AddObject(q);
                    o.SaveChanges();
                }
                dbt.Commit();
                o.AcceptAllChanges();
                lb.Text = "Record(s) saved.";
                view();
            }
            catch (Exception ex)
            {
                lb.Text = ex.Message.ToString();
                dbt.Rollback();
                o.Connection.Dispose();
                o.Connection.Close();
            }

        }

No comments:

Post a Comment