C#教程:COM+组件建立客户端程序
建立客户端程序
下面将建立客户端程序,设计结果如图1所示。详细步骤如下所示。

图1 客户端设计结果图
(1)创建一个项目,项目类型为Windows应用程序,设置其名称为35_02,单击【确定】按钮,项目创建成功。
(2)向Windows窗体添加5个TextBox控件和一个Button控件,它们分别用于输入银行转账信息和执行银行转账命令。
(3)引用EnterpriseServices。选择“项目”/“添加引用”,弹出“添加引用”对话框,在列表中选择System. EnterpriseServices,单击【确定】按钮即可。
(4)添加引用COM+服务组件——引用Ex35_01.DLL即可。选择“项目”/“添加引用”,弹出“添加引用”对话框,然后选择“浏览”选项卡,找到注册Ex35_01.DLL文件位置,如图2所示,最后单击【确定】按钮。

图2 添加COM+服务组件
(5)客户端实现代码。
首先,添加Ex35_01命名空间,其次,声明Transfer服务类对象,通过类对象调用银行转账BankTransfer方法,实现转账功能。完整代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Ex35_01;
namespace _5_02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Transfer tt = new Transfer();
bool bl= tt.BankTransfer(txtFromBank.Text, txtFromAccount.Text, txtToBank.Text, txtToAccount.Text, Single. Parse(txtBalance.Text));
if (bl)
MessageBox.Show("银行转账成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon. Information);
else
MessageBox.Show("银行转账失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
完整程序代码如下:
★★★★★Form1.cs窗体代码文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Ex35_01;
//using System.EnterpriseServices.CompensatingResourceManager;
namespace _5_02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Transfer tt = new Transfer();
bool bl= tt.BankTransfer(txtFromBank.Text, txtFromAccount.Text, txtToBank.Text, txtToAccount.Text, Single.Parse(txtBalance.Text));
if (bl)
MessageBox.Show("银行转账成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("银行转账失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
★ ★★★★Form1.Designer.cs窗体设计文件完整程序代码★★★★★
namespace _5_02
{
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.txtFromBank = new System.Windows.Forms.TextBox();
this.txtFromAccount = new System.Windows.Forms.TextBox();
this.txtToBank = new System.Windows.Forms.TextBox();
this.txtToAccount = new System.Windows.Forms.TextBox();