Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)qq
published in(发表于) 2014/7/11 9:24:05
C#教程:建立动态链接库

C#教程:建立动态链接库

建立动态链接库

创建动态链接库步骤如下所示。

(1)创建一个项目,项目类型为动态链接库,设置其名称为Ex35_01,单击【确定】按钮。

(2)引用EnterpriseServices,选择“项目”/“添加引用”,弹出“添加引用”对话框,如图1所示,在列表中选择System. EnterpriseServices,单击【确定】按钮即可。

(3)在“解决方案资源管理器”窗口中,将Class1.cs文件改名为Account.cs文件。

(4)在Account.cs程序文件中,引用命名空间System.Data、System.Data.SqlClient、using System.Diagnostics、System.EnterpriseServices,代码如下:



图1 引用EnterpriseServices

using System;

using System.Collections.Generic;

using System.Text;

using System.EnterpriseServices;

using System.Data;

using System.Data.SqlClient;

using System.Diagnostics;

namespace Ex35_01

{

public class Account

{

//在此编写类中的方法

}

}

自定义Saveing方法用于银行转账业务中的存款,并且将存款日志写入到Windows系统事件查看器中。代码如下:

public void Saveing(string bank, float balance, string account)

{

try

{

SqlConnection con = new SqlConnection("Server=(local);DataBase=db_35;uid=sa;pwd=;");

con.Open();

SqlCommand cmd = new SqlCommand("UPDATE " + bank + " set balance = balance + " + Convert.ToSingle (balance) + " WHERE account = '"+account+"'", con);

int i = (int)cmd.ExecuteNonQuery();

con.Close();

WriteInfo(DateTime.Now.ToString() + " 银行名称:" + bank + " 账号:" + account + "存入金额为:" + balance.ToString());

}

catch (Exception ex)

{

WriteError(ex.Message);

throw new Exception(ex.Message);

}

}

自定义Fetch方法用于银行转账业务中的取款,并且将存款日志写入到Windows系统事件查看器中。代码如下:

public void Fetch(string bank, float balance, string account)

{

try

{

if (balance > Convert.ToSingle(GetBalance(bank, balance, account)))

{

throw new Exception("银行:" + bank + " 账号:" + account + "余额不足!");

}

SqlConnection con = new SqlConnection("Server=(local);DataBase=db_35;uid=sa;pwd=;");

con.Open();

SqlCommand cmd = new SqlCommand("UPDATE " + bank + " SET balance = balance - " + Convert.ToSingle (balance) + " WHERE (account = '"+account+"')", con);

int i = (int)cmd.ExecuteNonQuery();

con.Close();

WriteInfo(DateTime.Now.ToString() + " 银行名称:" + bank + " 账号:" + account + "提取金额为:" + balance. ToString());

}

catch (Exception ex)

{

WriteError(ex.Message.ToString());

throw new Exception(ex.Message);

}

}

自定义GetBalance方法主要用于获取储蓄账号中的余额,并且在转账过程中判断余额是否充足。代码如下:

public Single GetBalance(string bank, float balance, string account)

{

SqlConnection con = new SqlConnection("Server=(local);DataBase=db_35;uid=sa;pwd=;");

SqlDataAdapter dap = new SqlDataAdapter("select * from " + bank + " where account ='" + account + "'", con);

DataSet ds = new DataSet();

dap.Fill(ds);

return Convert.ToSingle(ds.Tables[0].Rows[0]["balance"].ToString());

}

自定义WriteError方法主要将转账失败信息写入到Windows系统事件查看器中,代码如下:

public void WriteError(string error)

{

EventLog.WriteEntry("示例_01 COM+组件服务", error, EventLogEntryType.Error);

}

自定义WriteInfo方法主要将转账成功信息写入到Windows系统事件查看器中,代码如下:

public void WriteInfo(string info)

{

EventLog.WriteEntry("示例_01 COM+组件服务", info, EventLogEntryType.Information);

}

(5)编写转账类Transfer,添加一个类文件,并设置名称为Transfer.cs。在Transfer.cs程序文件中,引用命名空间System.EnterpriseServices,并且设置COM+组件的属性。代码如下:

using System;

using System.Collections.Generic;

using System.Text;

using System.EnterpriseServices;

namespace Ex35_01

{

//设置COM+属性

[Transaction(TransactionOption.RequiresNew)] //使用新事务创建组件


If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759