当前位置:编程学习 > C#/ASP.NET >>

linq to sql事务的一个问题

例如:我有两张表主表是[收款\付款单],外表是[结算明细表]

[收款\付款单]
(1)id
(2)往来单位
(3)业务日期
(4)单据编号
(5)说明

[结算明细表]
(1)id
(2)主表id
(3)结算账户id
(4)结算金额

我如何用linq to sql 的事务来做.

这要先让[收款\付款单]生成数据,给外表id.

db.SubmitChanges();后才能生成id.那后面的[结算明细表]又要db.SubmitChanges();一次。这就变成不是在同一个事务中了.

百度了很多没有找到相关的 --------------------编程问答-------------------- 等待回复.. --------------------编程问答-------------------- -.-等待... --------------------编程问答-------------------- 怎么还没有来帮助我 --------------------编程问答--------------------
有ORM,主从表可以一次性录入。

搜索 linq 一对多 --------------------编程问答-------------------- 我怎么觉得,应单据编号作为外键更合理一些,不知道对不对。 --------------------编程问答--------------------

 DataClassesDataContext db = DataClassesDataContext.Default;

        /// <summary>
        /// 增加课程
        /// </summary>
        /// <param name="web_id">站点id</param>
        /// <param name="coursename">课程名称</param>
        /// <param name="coursedesc">课程简介</param>
        /// <param name="message">提示信息</param>
        /// <returns></returns>
        /// 
        public bool AddCourse(long? comment_id, string coursename, string coursedesc, out string message)
        {
            if (db.T_COURSE.Any(c => c.COURSE_NAME == coursename||db.T_WEBSITE.Any(w => w.WEB_NAME == coursename)))
           {
               message = "已经存在此课程名称";
               return false;
           }
            else
            {
               if (db.Connection != null)
               {
                  db.Connection.Open();
               }
                DbTransaction tran = db.Connection.BeginTransaction();
                db.Transaction = tran;
                try
                {
                    long? a = db.LUserInners.FirstOrDefault(u => u.USER_ID == Potence.UserID).DEPART_ID;
                    T_WEBSITE web = new T_WEBSITE();
                    web.WEB_NAME = coursename;
                    web.WEBSIT_CLASS = 6;
                    web.DEPART_ID = a;
                    web.WEB_TYPE = "远程教育";
                    web.WEB_ABBRE_NAME = "course";
                    db.T_WEBSITE.InsertOnSubmit(web);
                    if (db.SaveSubmit())
                    {
                        T_COURSE course = new T_COURSE();
                        course.COMMENT_ID = comment_id;
                        course.COURSE_NAME = coursename;
                        course.COURSE_DESC = coursedesc;
                        course.WEB_ID = web.WEB_ID;
                        db.T_COURSE.InsertOnSubmit(course);
                     }
                    if (db.SaveSubmit())
                    {
                        tran.Commit();
                        message = "增加成功";
                        return true;
                    }
                    else
                    {
                        tran.Rollback();
                        message = "增加失败";
                        return false;
                    }

                }
                catch(Exception ex)
                {
                    message = ex.Message;
                    return false;
                }
            }
           
        }
--------------------编程问答-------------------- 上面的好像有错误,你看看这个以前写的:

/// <summary>
        /// 添加导航栏
        /// </summary>
        /// <param name="navigator">待添导航栏实体</param>
        /// <returns></returns>
        public static bool AddNavigator(T_NAVIGATOR navigator, List<long> roleList)
        {
            DataClasses1DataContext db = DataClasses1DataContext.Default;
            if (db.Connection != null)
            {
                db.Connection.Open();
            }
            DbTransaction tran = db.Connection.BeginTransaction();
            db.Transaction = tran;

            try//事务内容
            {
                if (IsExclusiveName(navigator.NAVIGATOR_NAME))//判断导航栏名是否已存在
                {
                    StringBuilder sqlStr = new StringBuilder();
                    db.T_NAVIGATOR.InsertOnSubmit(navigator);
                    db.SubmitChanges();//创建新导航栏
                    sqlStr.Append(db.ToSql());//创建导航栏日志
                    long navigatorID = navigator.NAVIGATOR_ID;//新导航栏ID
                    //List<T_NAVIGATE_ROLE> nr_List = new List<T_NAVIGATE_ROLE>();
                    for (int i = 0; i < roleList.Count; i++)
                    {
                        T_NAVIGATE_ROLE temp_NR = new T_NAVIGATE_ROLE() 
                        {
                            NAVIGATOR_ID = navigatorID,
                            ROLE_ID = roleList[i]
                        };
                        db.T_NAVIGATE_ROLE.InsertOnSubmit(temp_NR);//加入新的导航栏—角色数据

                        db.SubmitChanges();//更新导航栏—角色表
                        sqlStr.Append(db.ToSql());//更新导航栏—角色表日志
                    }                    
                                        
                    {//记录新增导航栏日志                       
                        T_LOG newLog = new T_LOG()
                        {
                            USER_ID = Potence.UserID,
                            OPER_DATE = DateTime.Now,
                            TABLE_NAME = "T_NAVIGATOR",
                            OPER_SQL = sqlStr.ToString(),
                            OPER_OBJ_ID = null,
                            LOG = "新增导航栏",
                            OPER_REASON = "新增导航栏",
                            OPER_OBJ_TYPE = OperType.新增.ToString(),
                            IP_ADDRESS = IPReader.IPAddress//记录IP地址
                        };
                        db.T_LOG.InsertOnSubmit(newLog);
                        db.SubmitChanges();
                    }
                    tran.Commit();//最终提交
                    return true;
                }
                else
                {
                    MessageBox.Show("导航栏名已存在", false);
                    return false;
                }
            }
            catch
            {
                tran.Rollback();//回滚
                return false;
            }
        }
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,