ASP.NET - 回滚事务

SqlConnection con =new SqlConnection(ConnectionDb.conStr);//获取数据库连接
        con.Open();//打开连接
        SqlTransaction sqltra = con.BeginTransaction();//开始事务
        SqlCommand cmd =new SqlCommand();//实例化
        cmd.Connection = con;//获取数据连接
        cmd.Transaction = sqltra;//,在执行SQL时,
try
        {
            string sql ="insert into OA_MEETING(meeting_id,meeting_name,meeting_title,start_time,end_time,announce_time,explain,reg_emp_id,emc_emp_id,par_emp_id,rep_emp_id,participate,MEETING_TYPE,PROJ_ID,WORK_ID)values('"+ id +"',N'"+ meetname +"',N'"+ meettitle +"','"+ starttime +"','"+ endtime +"',N'"+ djtime +"',N'"+ meetinfo +"','"+ empid +"','"+ meetperson +"','"+ lname +"','"+ writer +"','"+ ccname +"','"+ hidsort.Value +"','"+ proid +"','"+ workid +"')";
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();

            //string meetid = id;
int size =0;
            HttpFileCollection files = HttpContext.Current.Request.Files;
            //遍历上传文件窗体所有Html控件
foreach (string NameId in Request.Form)
            {
                //找到控件ID名前四位为"txt_"的文件说明文本框
if (NameId.Substring(0, 4) =="ttx_")
                {
                    //获取文本说明
if (Request.Form[NameId] !="")
                    {
                        string Explain = Request.Form[NameId];//获取txt文本说明

                        HttpPostedFile pstfile = files["fil_"+ NameId.Substring(4)];
                        string fileName ="";
                        string fileExtension ="";
                        fileName = System.IO.Path.GetFileName(pstfile.FileName);//上传的文件全名
                        fileExtension = System.IO.Path.GetExtension(fileName);//扩展名
string NewName = fileName.Remove(fileName.LastIndexOf("."));//去掉后缀名的文件名
string sql1 ="select isnull(max(app_id),0) from OA_MEETING_APPENDIX";
                       
                        int idd = Convert.ToInt32(objConnction.getString(sql1)) +1;
                        ua.Folder_Exists("Meeting");
                        string filepath ="..\\AtthFiles\\Meeting\\Meeting_"+ idd.ToString() + fileExtension;
                        string sqlstr1 ="insert into OA_MEETING_APPENDIX(APP_ID,MEETING_ID,EXPLAIN,APP_PATH) values('"+ idd +"','"+ id +"',N'"+ Explain +"',N'"+ filepath +"')";
                        cmd.CommandText = sqlstr1;

                        int num = cmd.ExecuteNonQuery();
                        if (num >0)
                        {
                            pstfile.SaveAs(Server.MapPath("..\\AtthFiles\\Meeting\\Meeting_"+ idd.ToString() + fileExtension));
                            size = pstfile.ContentLength /1024;
                            objDocument.InsertDoc(5, idd, Explain, "", "", DateTime.Now.ToString("yyyy-MM-dd"), Convert.ToInt32(HempID.Value), filepath, size, hidwork.Value, hidpro.Value);
                        }
                    }
                }
            }
            sqltra.Commit();
        }catch(Exception ex)
        {
            sqltra.Rollback();
        }

 

posted @ 2015-10-22 18:12  哪啊哪啊神去村  阅读(2390)  评论(0编辑  收藏  举报