EnterpriceServices

it2022-05-09  26

业务层:

业务层  1using System; 2using System.EnterpriseServices; 3 4using MyBusiness.Personnel; 5using MyBusiness.Orders; 6 7 8[assembly: ApplicationName("MyBusiness.Administration")] 9[assembly: ApplicationActivation(ActivationOption.Library)]1011namespace MyBusiness.Administration12{13    /**//// <summary>14    /// Epmployee Administration Object15    /// </summary>16     17    [ Transaction(TransactionOption.RequiresNew) ]18    [ ObjectPooling(true510) ]19    public class EmployeeMaintenance : ServicedComponent20    {21        public EmployeeMaintenance()22        {23        }2425        [ AutoComplete(true) ]26        public void AddEmployee(string Name, string Address, int JobType, bool bMakePayrollFail, bool bMakeOrdersFail)27        {28            // Create out tier 3 of 4 components that act as the data access layer.29            PayrollMaintenance payroll_maintenance = new PayrollMaintenance();30            OrdersMaintenance orders_maintenance = new OrdersMaintenance();3132            // Some business LogicNames must always be stored in upcase!33            Name = Name.ToUpper();3435            // Let the tier 3 of 4 access the seperate databases and store 36            // our complex business information.37            payroll_maintenance.AddEmployee(Name, Address, JobType, bMakePayrollFail);38            orders_maintenance.SetupUser(Name, JobType, bMakeOrdersFail);39        }40    }41}42 DAL层: Orders类DAL  1using System; 2using System.Data; 3using System.Data.SqlClient; 4using System.EnterpriseServices; 5 6[assembly: ApplicationName("MyBusiness.Orders")] 7[assembly: ApplicationActivation(ActivationOption.Library)] 8 9namespace MyBusiness.Orders10{11    /**//// <summary>12    /// Orders Specific Mainenance Object13    /// </summary>14    [ Transaction(TransactionOption.Required) ]15    [ ObjectPooling(true510) ]16    public class OrdersMaintenance : ServicedComponent17    {18        public OrdersMaintenance()19        {20        }2122        [ AutoComplete(true) ]23        public void SetupUser(string Name, int JobType, bool MakeFail)24        {25            string sConnection = "Persist Security Info=false;Data Source=192.168.0.98;Initial Catalog=MyOrdersDB;User ID=sa;Password=100200;";26            SqlConnection cnn= new SqlConnection(sConnection);2728            // Open the Database Connection29            cnn.Open();3031            DataSet ds = new DataSet();32            DataRow dr;33            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblOrderUser", cnn);34            SqlCommandBuilder cb = new SqlCommandBuilder(da);3536            // Tell it we want Primary and index keys.37            da.MissingSchemaAction = MissingSchemaAction.AddWithKey;3839            // Load the table from the database.40            // This will become slow very quickly, so not a great design,41            // but it does demonstrate the reading and writing of data.42            da.Fill(ds, "tblOrderUser");4344            dr = ds.Tables["tblOrderUser"].NewRow();4546            dr["sName"= Name;47            dr["nJobType"= JobType;48            dr["sTransactionActivityID"= ContextUtil.ActivityId;49            dr["sTransactionContextID"= ContextUtil.ContextId;5051            ds.Tables["tblOrderUser"].Rows.Add(dr);5253            da.Update(ds, "tblOrderUser");5455            // Close the Database Connection56            cnn.Close();5758            if(MakeFail)59            {60                // Oh no!!! Its all gone horibly wrong.61                throw new Exception("User requested Exception in PayrollMaintenance.AddEmployee");62            }63        }64    }65}66 Person类DAL  1using System; 2using System.Data; 3using System.Data.SqlClient; 4using System.EnterpriseServices; 5 6[assembly: ApplicationName("MyBusiness.Personnel")] 7[assembly: ApplicationActivation(ActivationOption.Library)] 8 9namespace MyBusiness.Personnel10{11    /**//// <summary>12    /// Payroll Specific Mainenance Object13    /// </summary>14    [ Transaction(TransactionOption.Required) ]15    [ ObjectPooling(true510) ]16    public class PayrollMaintenance : ServicedComponent17    {18        public PayrollMaintenance ()19        {20        }2122        public void AddEmployee(string Name, string Address, int JobType, bool MakeFail)23        {24            string sConnection = "Persist Security Info=false;Data Source=192.168.0.98;Initial Catalog=MyPersonnelDB;User ID=sa;Password=100200;";25            SqlConnection cnn= new SqlConnection(sConnection);2627            // Open the Database Connection28            cnn.Open();2930            DataSet ds = new DataSet();31            DataRow dr;32            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblEmployees", cnn);33            SqlCommandBuilder cb = new SqlCommandBuilder(da);3435            // Tell it we want Primary and index keys.36            da.MissingSchemaAction = MissingSchemaAction.AddWithKey;3738            // Load the table from the database.39            // This will become slow very quickly, so not a great design,40            // but it does demonstrate the reading and writing of data.41            da.Fill(ds, "tblEmployees");4243            dr = ds.Tables["tblEmployees"].NewRow();4445            dr["sName"= Name; ;46            dr["sAddress"= Address;47            dr["nJobType"= JobType;48            dr["sTransactionActivityID"= ContextUtil.ActivityId;49            dr["sTransactionContextID"= ContextUtil.ContextId;5051            ds.Tables["tblEmployees"].Rows.Add(dr);5253            da.Update(ds, "tblEmployees");5455            // Close the Database Connection56            cnn.Close();5758            if(MakeFail)59            {60                // Oh no!!! Its all gone horibly wrong.61                throw new Exception("User requested Exception in PayrollMaintenance.AddEmployee");62            }63        }64    }65}66 客户: 客户代码  1EmployeeMaintenance employee_maintenance = new EmployeeMaintenance(); 2 3            // Its a one function wonder, but we could call a few. The 4            // transaction would succeed as long as they all voted yes. 5            // Remeber the transaction state lived with the life of the 6            // object and the transaction is commited or rolled back  7            // when it goes out of scope. 8 9            try10            {1112                employee_maintenance.AddEmployee13                    (14                    txtEmployee.Text, 15                    txtAddress.Text, 16                    Convert.ToInt32(txtJobType.Text), 17                    cbExceptionPersonnel.Checked, 18                    cbExceptionOrders.Checked19                    );2021            }22            catch(Exception ex)23            {24                string sMessage = "The transaction threw the follwing Exception:\n\n";25                26                sMessage += ex.Message + "\n";27                sMessage += ex.Source + "\n";28                sMessage += "\nThe transaction will be rolled back.";2930                MessageBox.Show(sMessage, "Unhandled Exeption");3132                throw ex;33            }

转载于:https://www.cnblogs.com/nanshouyong326/archive/2006/11/27/574120.html

相关资源:数据结构—成绩单生成器

最新回复(0)