bankclient.cs
来自「中间件编写示例 COM与.NET组件服务」· CS 代码 · 共 283 行
CS
283 行
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using BankAccountServer;
namespace BankClient
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class BankClient : Form
{
private PictureBox pictureBox1;
private Button transferButton;
private TextBox sourceBox;
private Label sourceLabel;
private Label destLable;
private TextBox destBox;
private TextBox amountBox;
private Label amountLable;
private DataGrid accountsGrid;
private SqlCommand sqlSelectCommand;
private SqlCommand sqlInsertCommand;
private SqlCommand sqlUpdateCommand;
private SqlCommand sqlDeleteCommand;
private SqlConnection connection;
private SqlDataAdapter adapter;
private AccountsDataSet accountsDataSet;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public BankClient()
{
InitializeComponent();
connection.Open();
adapter.Fill(accountsDataSet);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(BankClient));
this.amountBox = new System.Windows.Forms.TextBox();
this.destLable = new System.Windows.Forms.Label();
this.transferButton = new System.Windows.Forms.Button();
this.connection = new System.Data.SqlClient.SqlConnection();
this.sqlSelectCommand = new System.Data.SqlClient.SqlCommand();
this.destBox = new System.Windows.Forms.TextBox();
this.sourceLabel = new System.Windows.Forms.Label();
this.accountsDataSet = new AccountsDataSet();
this.sqlInsertCommand = new System.Data.SqlClient.SqlCommand();
this.sqlDeleteCommand = new System.Data.SqlClient.SqlCommand();
this.amountLable = new System.Windows.Forms.Label();
this.accountsGrid = new System.Windows.Forms.DataGrid();
this.sqlUpdateCommand = new System.Data.SqlClient.SqlCommand();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.adapter = new System.Data.SqlClient.SqlDataAdapter();
this.sourceBox = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.accountsDataSet)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.accountsGrid)).BeginInit();
this.SuspendLayout();
//
// amountBox
//
this.amountBox.Location = new System.Drawing.Point(272, 40);
this.amountBox.Name = "amountBox";
this.amountBox.Size = new System.Drawing.Size(80, 20);
this.amountBox.TabIndex = 2;
this.amountBox.Text = "100";
//
// destLable
//
this.destLable.Location = new System.Drawing.Point(144, 16);
this.destLable.Name = "destLable";
this.destLable.Size = new System.Drawing.Size(104, 23);
this.destLable.TabIndex = 3;
this.destLable.Text = "Destination Acount:";
//
// transferButton
//
this.transferButton.Location = new System.Drawing.Point(392, 40);
this.transferButton.Name = "transferButton";
this.transferButton.TabIndex = 1;
this.transferButton.Text = "Transfer";
this.transferButton.Click += new System.EventHandler(this.OnTransfer);
//
// connection
//
this.connection.ConnectionString = "data source=A20M;initial catalog=Bank Account System;integrated security=SSPI;per" +
"sist security info=False;workstation id=A20M;packet size=4096";
//
// sqlSelectCommand
//
this.sqlSelectCommand.CommandText = "SELECT Number, Balance, Name FROM dbo.BankAccounts";
this.sqlSelectCommand.Connection = this.connection;
//
// destBox
//
this.destBox.Location = new System.Drawing.Point(144, 40);
this.destBox.Name = "destBox";
this.destBox.TabIndex = 2;
this.destBox.Text = "456";
//
// sourceLabel
//
this.sourceLabel.Location = new System.Drawing.Point(8, 16);
this.sourceLabel.Name = "sourceLabel";
this.sourceLabel.TabIndex = 3;
this.sourceLabel.Text = "Source Acount:";
//
// accountsDataSet
//
this.accountsDataSet.DataSetName = "AccountsDataSet";
this.accountsDataSet.Locale = new System.Globalization.CultureInfo("en-US");
this.accountsDataSet.Namespace = "http://www.tempuri.org/AccountsDataSet.xsd";
//
// sqlInsertCommand
//
this.sqlInsertCommand.CommandText = "INSERT INTO dbo.BankAccounts(Number, Balance, Name) VALUES (@Number, @Balance, @N" +
"ame); SELECT Number, Balance, Name FROM dbo.BankAccounts WHERE (Number = @Select" +
"_Number)";
this.sqlInsertCommand.Connection = this.connection;
this.sqlInsertCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Number", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Number", System.Data.DataRowVersion.Current, null));
this.sqlInsertCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Balance", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Balance", System.Data.DataRowVersion.Current, null));
this.sqlInsertCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Name", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Name", System.Data.DataRowVersion.Current, null));
this.sqlInsertCommand.Parameters.Add(new System.Data.SqlClient.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));
//
// sqlDeleteCommand
//
this.sqlDeleteCommand.CommandText = "DELETE FROM dbo.BankAccounts WHERE (Number = @Number) AND (Balance = @Balance OR " +
"@Balance1 IS NULL AND Balance IS NULL) AND (Name = @Name OR @Name1 IS NULL AND N" +
"ame IS NULL)";
this.sqlDeleteCommand.Connection = this.connection;
this.sqlDeleteCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Number", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Number", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Balance", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Balance", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Balance1", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Balance", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Name", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Name", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Name1", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Name", System.Data.DataRowVersion.Original, null));
//
// amountLable
//
this.amountLable.Location = new System.Drawing.Point(272, 16);
this.amountLable.Name = "amountLable";
this.amountLable.Size = new System.Drawing.Size(192, 23);
this.amountLable.TabIndex = 3;
this.amountLable.Text = "Amount:";
//
// accountsGrid
//
this.accountsGrid.DataMember = "";
this.accountsGrid.DataSource = this.accountsDataSet.BankAccounts;
this.accountsGrid.Location = new System.Drawing.Point(8, 104);
this.accountsGrid.Name = "accountsGrid";
this.accountsGrid.Size = new System.Drawing.Size(264, 216);
this.accountsGrid.TabIndex = 4;
//
// sqlUpdateCommand
//
this.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)";
this.sqlUpdateCommand.Connection = this.connection;
this.sqlUpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Number", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Number", System.Data.DataRowVersion.Current, null));
this.sqlUpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Balance", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Balance", System.Data.DataRowVersion.Current, null));
this.sqlUpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Name", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, true, ((System.Byte)(0)), ((System.Byte)(0)), "Name", System.Data.DataRowVersion.Current, null));
this.sqlUpdateCommand.Parameters.Add(new System.Data.SqlClient.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));
this.sqlUpdateCommand.Parameters.Add(new System.Data.SqlClient.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));
this.sqlUpdateCommand.Parameters.Add(new System.Data.SqlClient.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));
this.sqlUpdateCommand.Parameters.Add(new System.Data.SqlClient.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));
this.sqlUpdateCommand.Parameters.Add(new System.Data.SqlClient.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));
this.sqlUpdateCommand.Parameters.Add(new System.Data.SqlClient.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));
//
// pictureBox1
//
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox1.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(328, 104);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(144, 128);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// adapter
//
this.adapter.DeleteCommand = this.sqlDeleteCommand;
this.adapter.InsertCommand = this.sqlInsertCommand;
this.adapter.SelectCommand = this.sqlSelectCommand;
this.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")})});
this.adapter.UpdateCommand = this.sqlUpdateCommand;
//
// sourceBox
//
this.sourceBox.Location = new System.Drawing.Point(8, 40);
this.sourceBox.Name = "sourceBox";
this.sourceBox.TabIndex = 2;
this.sourceBox.Text = "123";
//
// BankClient
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 333);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.accountsGrid,
this.sourceLabel,
this.sourceBox,
this.transferButton,
this.pictureBox1,
this.destLable,
this.destBox,
this.amountBox,
this.amountLable});
this.Name = "BankClient";
this.Text = "Bank Client";
((System.ComponentModel.ISupportInitialize)(this.accountsDataSet)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.accountsGrid)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new BankClient());
}
private void OnTransfer(object sender,EventArgs e)
{
int debitAccount;
int creditAccount;
decimal amount;
debitAccount = Convert.ToInt32(sourceBox.Text);
creditAccount = Convert.ToInt32(destBox.Text);
amount = Convert.ToDecimal(amountBox.Text);
try
{
IAccountMgr accountManager;
accountManager = new AccountMgr();
accountManager.Transfer(debitAccount,creditAccount,amount);
}
catch(Exception exception)
{
MessageBox.Show("Some error occurred: "+exception.Message,"Bank Client");
}
finally
{
adapter.Fill(accountsDataSet);
accountsGrid.Refresh();
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?