本文介绍了删除未保存在insertonsubmitchanges方法上的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
我定义了一个带有insert方法的类,用于将数据插入sqlserver数据库.
我有2张桌子,其中一张是另一张的子代.
它们之间有关系.

我想将一个实体(记录)插入父表,并向其子表插入一个或某些记录,即子表的记录具有父表的记录的一个forekey.
我定义了两个类:
1.名为"StudentBLL"的第一堂课,用于插入,更新,删除学生.
2.第二类名为"TeacherBLL",用于插入,更新,删除教师.
见下文:

hello every body.
I defined a class with insert method for insert data to sqlserver database.
I have 2 table that one of them is child of another.
There is a relation between them.

i want to insert an entity(record) to parent table and one or some record to it''s child table that child table''s records have one forenkey of parent table''s record.
I define two class:
1.First class named ‘StudentBLL’ ,for insert,update,delete students.
2.Second class named ‘TeacherBLL’ for insert,update,delete teachers.
See below :

class TeacherBLL
    {
        DataBaseDataContext _db;
        public TeacherBLL(DataBaseDataContext db)
        {
            _db = db;
        }
        public bool Insert(Teacher _teacher, Students[] _students)
        {
            using(TransactionScope ts=new TransactionScope())
            {
                _db.tbl_plaques.InsertOnSubmit(_teacher);
                try
                {
                    _db.SubmitChanges();
                    if (new StudentBLL(_db).Insert(_students))
                    {
                        //Transaction Commit
                        ts.Complete();
                        _db.Dispose();
                        return true;
                    }
                    else
                    {
                        //Transaction  Rollback
                        return false;
                    }
                }
                catch (Exception e)
                {
                    // Transaction Rollback
                    System.Windows.Forms.MessageBox.Show(e.Message);
                    return false;
                }
            }
        }
    }







class StudentBLL
    {
        DataBaseDataContext _db;
        public StudentBLL(DataBaseDataContext db)
        {
            _db = db;
        }
        public bool Insert(Students[] _students)
        {
            _db.tbl_plaques.InsertAllOnSubmit(_teacher);
            try
            {
                _db.SubmitChanges();
                System.Windows.Forms.MessageBox.Show("Data Inserted Successfully...");
                return true;
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
                return false;
            }
        }
    }







public partial class Form1 : Form
    {
        DataBase.DataClasses1DataContext _db;
        public Form1()
        {
            InitializeComponent();
            _db = new DataBase.DataClasses1DataContext();
        }
        private void ButtonSave_Click(object sender, EventArgs e)
        {
		Teacher _Teacher=new Teacher(){
			ID=txt_ID.Text,
			Gender=txt_Gender.Text,
			Name=txt_Name.Text};
		Student[] _Students=new Student[10];
		.
                .
                .
                .
                .
                if (new TeacherBLL(_db).Insert(_Teacher,_Students))
                {
                       this.Close();
                }

         }
}


当用户单击保存按钮时,我创建了Teacher BLL类的对象并调用了insert方法,
在插入方法中,我插入教师实体并提交我的数据上下文的更改,并创建一个StudentBLL对象,并为每个学生项目调用插入方法.(出于某些原因)
我在插入教师之前创建了一个事务,并在学生插入成功后提交了事务,或者如果其中一个学生插入有错误,则回滚(甚至不保存老师和其他学生).

请注意,我的数据上下文对象以表单形式创建,并在其构造函数中传递给了Teacherbll(出于某种原因),也传递给了Studentbll.因此,如果保存操作出错并以后将其发布到TeacherBLL和StudentBLL,则我的数据上下文对象不会被释放.
如果用户尝试在第一次保存后保存数据而没有关闭格式,则出现以下错误:
插入TeacherBLL中方法插入的第一行:


When user click save button, I create an object of Teacher BLL class and call insert method,
In insert method ,I insert teacher entity and submit my data context’s changes and create one object of StudentBLL and call insert method for each student item.( for some reasons )
I create one transaction before insert teacher and commit transaction after students inserted successfully or rollback if one of students insert has error ( Even save not Teacher and other students).

Notice that my data context object created in form and is passed to teacherbll( for some reasons) in it’s constructor and to studentbll too. So my data context object not disposed if save action has error and posts to TeacherBLL and StudentBLL in future.
If user try to save data after first save without close form, occur below error :
on line 1 of method insert in TeacherBLL :

_db.tbl_plaques.InsertOnSubmit(_teacher);


在我尝试第二次保存数据之前,请检查数据库表,并且没有保存先前保存操作中的任何项目,因此在第二次保存操作中,我需要删除在第一次保存操作中已保存的教师项目,以避免重复数据错误.

请帮助我:
我该如何解决这个问题?
我如何删除该记录以解决该错误.

非常感谢....!


before i try to save data in second,i check database tables and no items in previous save action were saved,so in second save action i need to remove teacher item that in first save action was saved to avoid Duplicated Data error.

please help me :
how can i solve this problem?
how can i remove that record to solve that error.

thanks alot.... !

推荐答案

Using(TransctoinScope ts = new TransactionScope())
{
       try{
          db.SubmitChanges();
          ts.Complete();
       }
       catch
       {
          
       }
}



您可以遍历MSDN中提供的演练.它为您提供了一种非常清晰简单的使用事务的方法.



You can go through the walk through provided in MSDN. It provides you with a very clear and simple methods of using transactions.


using (System.Transactions.CommittableTransaction transaction = new System.Transactions.CommittableTransaction())
{
    //...

    if(new TeacherBLL(_db).Insert(_Teacher,_Students)))
       transaction.Commit();
    else
       transaction.Rollback();       
}



我不知道您的代码(TracherBLL(_db),Insert)是否有效,但如果可以,则可以按照我的示例使用它.



I Don''t know that your code (TracherBLL(_db),Insert) works or not but if it''s ok then you can use it as I''ve already exampled.



这篇关于删除未保存在insertonsubmitchanges方法上的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:47