C#使用SqlConnection的事务SqlTransaction



        /// 
        /// Pacs发送数据到新***老系统
        /// 
        /// 
        public static void SendSample2XHYYPacs(string sglcheckid)
        {
            if (string.IsNullOrWhiteSpace(sglcheckid)) { return; }
            //CYYF_V_ApplicationInfo我们的在老系统中的视图
            //V_HCS_APPLICATIONINFO我们的ORACLE视图(老系统中原有视图就是这个名称,并行阶段老系统这个视图包含了我们的视图)
            string sql_query = string.Format("SELECT * FROM V_HCS_APPLICATIONINFO WHERE HISPATIENTID='{0}'", string.Format("H{0}", sglcheckid));
            DataTable dt_V_HCS_APPLICATIONINFO = BLLUtil.DoQueryBySql(sql_query);
            if (dt_V_HCS_APPLICATIONINFO == null || dt_V_HCS_APPLICATIONINFO.Rows.Count < 1) { return; }
            string sql_insert = "";
            foreach (DataRow dr_V_HCS_APPLICATIONINFO in dt_V_HCS_APPLICATIONINFO.Rows)
            {
                sql_insert += string.Format(@"INSERT INTO medbase.dbo.CYYF_V_ApplicationInfo
(HisPatientID, HisPatientType, SocietyID, eNetPatientID, Phonenumber,
ApplicationID, PatientName, Sex, Birthday, IDnumber,
Address, Folk, Nationality, ReqHospital, ReqDepartmentName, 
ReqDoctorName, ExeDept, DeviceTypeName, DeviceName, OperatorTime, 
HisCheckItem, Abstracthistory, Diagnose, Cost, BodyPartName, 
StudyMethodName, IsFee, IsConfirm,ENTERPRICE,DEPTNO,EMPNO) 
VALUES ( 
'{0}', '{1}', '', '{18}', '{19}',
'{2}', '{3}', '{4}', '{5}', '{6}',
'', '', '', '{7}', '{8}', 
'{9}', '{20}', '{10}', '', '{11}', 
'{12}', '{21}', '{22}', {13}, '{14}', 
'{15}',{16},{17},'{23}','{24}','{25}');
",
 dr_V_HCS_APPLICATIONINFO["HisPatientID"], dr_V_HCS_APPLICATIONINFO["HisPatientType"], dr_V_HCS_APPLICATIONINFO["ApplicationID"], dr_V_HCS_APPLICATIONINFO["PatientName"], dr_V_HCS_APPLICATIONINFO["Sex"],
 dr_V_HCS_APPLICATIONINFO["Birthday"], dr_V_HCS_APPLICATIONINFO["IDnumber"], dr_V_HCS_APPLICATIONINFO["ReqHospital"], dr_V_HCS_APPLICATIONINFO["ReqDepartmentName"], dr_V_HCS_APPLICATIONINFO["ReqDoctorName"],
 dr_V_HCS_APPLICATIONINFO["DeviceTypeName"], dr_V_HCS_APPLICATIONINFO["OperatorTime"], dr_V_HCS_APPLICATIONINFO["HisCheckItem"], dr_V_HCS_APPLICATIONINFO["Cost"], dr_V_HCS_APPLICATIONINFO["BodyPartName"],
 dr_V_HCS_APPLICATIONINFO["StudyMethodName"], dr_V_HCS_APPLICATIONINFO["IsFee"], dr_V_HCS_APPLICATIONINFO["IsConfirm"], dr_V_HCS_APPLICATIONINFO["eNetPatientID"]
 , dr_V_HCS_APPLICATIONINFO["Phonenumber"], dr_V_HCS_APPLICATIONINFO["ExeDept"], dr_V_HCS_APPLICATIONINFO["Abstracthistory"], dr_V_HCS_APPLICATIONINFO["Diagnose"], dr_V_HCS_APPLICATIONINFO["ENTERPRICE"], dr_V_HCS_APPLICATIONINFO["DEPTNO"], dr_V_HCS_APPLICATIONINFO["EMPNO"]
 );
            }
            try
            {
                using (SqlConnection sqlCon = new SqlConnection(string.Format("Data Source=*.*.9.155;Initial Catalog=medbase;User ID=*;Password=*;")))
                //using (SqlConnection sqlCon = new SqlConnection(string.Format("Data Source=localhost;Initial Catalog=medbase;User ID=*;Password=*;")))
                {
                    sqlCon.Open();
                    SqlTransaction sqlTransaction = sqlCon.BeginTransaction();  
                    SqlCommand com = new SqlCommand(sql_insert, sqlCon, sqlTransaction);
                    com.ExecuteNonQuery();
                    //MessageBox.Show("OK");
                    sqlTransaction.Commit();  
                }
            }
            catch (Exception e1)
            {
                Console.WriteLine(string.Format("{0}{1}", e1.Message, "错误"));//.Show(e1.Message, "错误");
            }
        }

        /// 
        /// Lis发送数据到新**老系统
        /// 
        /// 
        public static void SendSample2XHYYLis(string sglcheckid)
        {
            if (string.IsNullOrWhiteSpace(sglcheckid)) { return; }
            string sql_query = string.Format("SELECT * FROM LIS_REQ WHERE 病人代号 ='{0}'", sglcheckid);
            DataTable dt_LIS_REQ = BLLUtil.DoQueryBySql(sql_query);
            if (dt_LIS_REQ == null || dt_LIS_REQ.Rows.Count < 1) { return; }
            string sql_insert = "";
            foreach (DataRow dr_LIS_REQ in dt_LIS_REQ.Rows)
            {
                sql_insert += string.Format(@"INSERT INTO medbase.dbo.CYYF_LIS_REQ
([病人代号], [姓名], [性别], [年龄], [生日], [申请时间], [项目通道号], [项目代码], [项目名称], [单位], [分组], WYH, [申请医生], [状态], [计价状态], [病人来源], [急诊标志], [床号], [诊断], [样本], [执行医生], [执行时间], [采样时间], [报告单代号], [备注], [地址], [费别], [医嘱号], [单价], [数量], [个人状态]) 
VALUES ('{0}', '{1}', '{2}', {3}, '', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}',
'{10}', '', '1', '1', 4, '', '', '', '', '', 
'{11}', '{12}', '', '', '', '', '', '', '1', '1040')
;",
 dr_LIS_REQ["病人代号"],
 dr_LIS_REQ["姓名"],
 dr_LIS_REQ["性别"],
 dr_LIS_REQ["年龄"],
 dr_LIS_REQ["申请时间"],
 dr_LIS_REQ["项目通道号"],
 dr_LIS_REQ["项目代码"],
 dr_LIS_REQ["项目名称"],
 dr_LIS_REQ["单位"],
 dr_LIS_REQ["分组"],
 dr_LIS_REQ["WYH"],
 dr_LIS_REQ["执行时间"],
 dr_LIS_REQ["采样时间"]);
            }
            try
            {
                using (SqlConnection sqlCon = new SqlConnection(string.Format("Data Source=*.*.9.155;Initial Catalog=medbase;User ID=*;Password=*;")))
                //using (SqlConnection sqlCon = new SqlConnection(string.Format("Data Source=localhost;Initial Catalog=medbase;User ID=*;Password=*;")))
                {
                    sqlCon.Open();
                    SqlTransaction sqlTransaction = sqlCon.BeginTransaction();  
                    SqlCommand com = new SqlCommand(sql_insert, sqlCon, sqlTransaction);
                    com.ExecuteNonQuery();
                    //MessageBox.Show("OK");
                    sqlTransaction.Commit();  
                }
            }
            catch (Exception e1)
            {
                Console.WriteLine(string.Format("{0}{1}", e1.Message, "错误"));
            }
        }

        /// 
        /// 
        /// 
        /// 
        public static void DeleteSample2XHYYLis(string sglcheckid)
        {
            if (string.IsNullOrWhiteSpace(sglcheckid)) { return; }
            string sql_delete = string.Format("DELETE CYYF_LIS_REQ WHERE [病人代号] = '{0}'", sglcheckid);
            try
            {
                using (SqlConnection sqlCon = new SqlConnection(string.Format("Data Source=*.*.9.155;Initial Catalog=medbase;User ID=*;Password=*;")))
                //using (SqlConnection sqlCon = new SqlConnection(string.Format("Data Source=localhost;Initial Catalog=medbase;User ID=*;Password=*;")))
                {
                    sqlCon.Open();
                    SqlTransaction sqlTransaction = sqlCon.BeginTransaction();  
                    SqlCommand com = new SqlCommand(sql_delete, sqlCon, sqlTransaction);
                    com.ExecuteNonQuery();
                    //MessageBox.Show("OK");
                    sqlTransaction.Commit();  
                }
            }
            catch (Exception e1)
            {
                Console.WriteLine(string.Format("{0}{1}", e1.Message, "错误"));
            }
        }
    }
}

你可能感兴趣的:(C#使用SqlConnection的事务SqlTransaction)