⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 accounts.cs

📁 中间件编写示例 COM与.NET组件服务
💻 CS
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.EnterpriseServices;


namespace BankAccountServer
{
	public interface IAccountMgr
	{
		void Transfer(int debitAccount,int creditAccount,decimal amount);
	}
	interface IAccount
	{
		void Credit(int accountNumber,decimal amount);
		void Debit(int accountNumber,decimal amount);
	}
	
	[Transaction]
	public class AccountMgr : ServicedComponent,IAccountMgr
	{
		[AutoComplete]
		public void Transfer(int debitAccount,int creditAccount,decimal amount)
		{
			Account creditAccountObj;
			Account debitAccountObj;

			creditAccountObj = new Account();
			debitAccountObj = new Account();

			creditAccountObj.Credit(creditAccount,amount); 
			debitAccountObj.Debit(debitAccount,amount);
		}
	}
	[Transaction]
	internal class Account : ServicedComponent,IAccount
	{
		private SqlCommand sqlSelectCommand;
		private SqlCommand sqlUpdateCommand;
		private SqlConnection connection;
		private SqlDataAdapter adapter;
		private AccountsDataSet accountsDataSet;
		private void InitDataSource()
		{
			sqlSelectCommand = new SqlCommand();
			sqlUpdateCommand = new SqlCommand();
			connection = new SqlConnection();
			adapter = new SqlDataAdapter();
			accountsDataSet = new AccountsDataSet();
			((System.ComponentModel.ISupportInitialize)(accountsDataSet)).BeginInit();

			// sqlSelectCommand
			sqlSelectCommand.CommandText = "SELECT Number, Balance, Name FROM dbo.BankAccounts";
			sqlSelectCommand.Connection = connection;
		
			// sqlUpdateCommand
			sqlUpdateCommand.CommandText = @"UPDATE dbo.BankAccounts SET Number = @Number, Balance = @Balance, Name = @Name WHERE (Number = @Original_Number) AND (Balance = @Original_Balance OR @Original_Balance1 IS NULL AND Balance IS NULL) AND (Name = @Original_Name OR @Original_Name1 IS NULL AND Name IS NULL); SELECT Number, Balance, Name FROM dbo.BankAccounts WHERE (Number = @Select_Number)";
			sqlUpdateCommand.Connection = connection;
			sqlUpdateCommand.Parameters.Add(new SqlParameter("@Number", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Number", System.Data.DataRowVersion.Current, null));
			sqlUpdateCommand.Parameters.Add(new SqlParameter("@Balance", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Balance", System.Data.DataRowVersion.Current, null));
			sqlUpdateCommand.Parameters.Add(new SqlParameter("@Name", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Name", System.Data.DataRowVersion.Current, null));
			sqlUpdateCommand.Parameters.Add(new SqlParameter("@Original_Number", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Number", System.Data.DataRowVersion.Original, null));
			sqlUpdateCommand.Parameters.Add(new SqlParameter("@Original_Balance", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Balance", System.Data.DataRowVersion.Original, null));
			sqlUpdateCommand.Parameters.Add(new SqlParameter("@Original_Balance1", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Balance", System.Data.DataRowVersion.Original, null));
			sqlUpdateCommand.Parameters.Add(new SqlParameter("@Original_Name", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Name", System.Data.DataRowVersion.Original, null));
			sqlUpdateCommand.Parameters.Add(new SqlParameter("@Original_Name1", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Name", System.Data.DataRowVersion.Original, null));
			sqlUpdateCommand.Parameters.Add(new SqlParameter("@Select_Number", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Number", System.Data.DataRowVersion.Current, null));
			
			// connection
			connection.ConnectionString = "data source=A20M;initial catalog=Bank Account System;integrated security=SSPI;per" +
				"sist security info=True;workstation id=A20M;packet size=4096";
			
			// adapter
			adapter.SelectCommand = sqlSelectCommand;
			adapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
																							  new System.Data.Common.DataTableMapping("Table", "BankAccounts", new System.Data.Common.DataColumnMapping[] {
																																																			  new System.Data.Common.DataColumnMapping("Number", "Number"),
																																																			  new System.Data.Common.DataColumnMapping("Balance", "Balance"),
																																																			  new System.Data.Common.DataColumnMapping("Name", "Name")})});
			adapter.UpdateCommand = sqlUpdateCommand;

			// accountsDataSet
			accountsDataSet.DataSetName = "AccountsDataSet";
			accountsDataSet.Locale = new System.Globalization.CultureInfo("en-US");
			accountsDataSet.Namespace = "http://www.tempuri.org/AccountsDataSet.xsd";
		}
		public Account()
		{
			InitDataSource();
			connection.Open();
			adapter.Fill(accountsDataSet,"BankAccounts");
		}

		[AutoComplete]
		public void Credit(int accountNumber,decimal amount)
		{
			AccountsDataSet.BankAccountsDataTable table = accountsDataSet.BankAccounts;
			AccountsDataSet.BankAccountsRow row = table.FindByNumber(accountNumber);				
			row.Balance += amount;
			adapter.Update(table);
		}

		[AutoComplete]
		public void Debit(int accountNumber,decimal amount)
		{
			AccountsDataSet.BankAccountsDataTable table = accountsDataSet.BankAccounts;
			AccountsDataSet.BankAccountsRow row = table.FindByNumber(accountNumber);
			if(row.Balance >= amount)
			{
				row.Balance -= amount;
			}
			else
			{
				InvalidOperationException exception;
				string msg = @"Debit amount is greater than balance
                               in account #" + accountNumber.ToString();
				exception = new InvalidOperationException(msg);
				throw(exception);
			}
			adapter.Update(table);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -