使用C#生成dll文件并调用
一、创建dll文件:
例如生成一个md5编码判断状态的文件,即,输入一个字符串(string A)和一个32位md5编码(string B),判断此字符串A对应的32位md5编码是否与B相等,如果相等返回true,否则返回false。
打开VS 2005,“文件”--》“新建”--“项目”,选择“Windows 控件库”,命名后点击“确定”,在“UserControl1.cs”中输入以下代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Text;
using System.Security.Cryptography;
namespace md5
{
public partial class Program : UserControl
{
#region MD5 32位加密:GetMd5Str32
/// <summary>
/// 32位MD5加密
/// </summary>
/// <param name="strSource">待加密字串</param>
/// <returns>加密后的字串</returns>
public static string GetMd5Str32(string strSource)
{
byte[] bytes = Encoding.ASCII.GetBytes(strSource);
byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 16; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}
return sb.ToString().ToUpper();
}
#endregion
#region 核对md5编码是否一致:CheckMd5String()
/// <summary>
/// 核对md5编码是否一致
/// </summary>
/// <param name="ConvertString"></param>
/// <returns>如果一致返回true,否则返回false</returns>
///
public static bool CheckMd5String(string str1, string str2)
{
string md5String = str1; //需要验证的字符串
string md5DbString = str2; //需要核对的32位md5编码
int result = string.Compare(md5.Program.GetMd5Str32(str1), md5DbString, true);
if (result == 0)
{
return true;
}
else
{
return false;
}
}
#endregion
}
}
修改“UserControl1.Designer.cs”中的命名空间为“md5”,方法为“Program”,即可生成dll文件。
在...\bin\Debug文件假下,可以找到相应的dll文件。
二、部署dll流程:
首先把dll文件放到应用程序...\bin\Debug\下;
然后在解决方案中添加引用:右键鼠标-->添加引用-->浏览-->选择dll放置路径后点击“确定”。
注意:要在应用文件头处使用using md5;命令。
测试应用程序代码,如下: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 md5;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str1 = textBox1.Text.ToString();
string md5String = textBox2.Text.ToString();
textBox3.Text = md5.Program.GetMd5Str32(str1);
textBox4.Text = md5.Program.CheckMd5String(str1, md5String).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
2. mybatis sql属性用sql标签封装出问题了 应该怎么改
username=#{1},password=#{2} where id=#{0}这些代码写的有问题,应该是对应的UserInfo里面的属性名,如useName,password,id等。
3. c# sqlDataReader 封装调用问题(调用出错 错误提示如图)
///
<summary>
///
执行查询语句,返回SqlDataReader
(
注意:调用该方法后,一定要对SqlDataReader进行Close
)
///
</summary>
///
<param
name="strSQL">查询语句</param>
///
<returns>SqlDataReader</returns>
public
static
SqlDataReader
ExecuteReader(string
strSQL)
{
SqlConnection
connection
=
new
SqlConnection(connectionString);
SqlCommand
cmd
=
new
SqlCommand(strSQL,
connection);
try
{
connection.Open();
SqlDataReader
myReader
=
cmd.ExecuteReader(CommandBehavior.CloseConnection);
return
myReader;
}
catch
(System.Data.SqlClient.SqlException
e)
{
throw
e;
}
}
上面的代码是微软的数据库操作类中此部分内容的写法
您可以参考
您的问题我感觉应该是你外部程序的问题
看您此处的代码
暂时还没发现问题
4. 9.6 如何封装sql 数据库命令举例
做成 存储过程
create procere sp_procere --创建存储过程
(@key varchar(10)) --设置传递的参数(可以没有)
as
begin
--需封装的sql语句
end
运行该存储过程
exec sp_procere 参数
5. hibernate 用sql完成多表查询的结果集如何封装
将student,class关联创建一个视图,然后createSQLQuery("查询视图")
.setResultTransformer(Transformers.aliasToBean(视图VO.class))
.list();
这样就万事大吉了
6. 大神们,膝盖奉上了~sql server如何将多条语句封装为一个存储过程
可以的,你把软件发过来,我帮你研究一下怎么封装
7. Sql2000数据库怎样封装
直接对数据库封装?好象不行吧,最多你把代码封装,数据库里把权限设置好,别人也直接进不来,然后在对一些存储过程加密.
8. mybatis是如何将sql执行结果封装为目标对象并返回的都有哪些映射形式
是写一个resultmap然后指明字段和属性映射关系就行
9. sql2005:如何封装一些重用性高的sql语句块
把一些经常用到的存储过程放到一个程序包里面,相当于java里面的utils包,以后要调用的时候,直接包名.存储过程就行了。
比如,现在我建立一个最简单的工具包:
createorreplacepackagePKG_COMM_UTILSis
procereconsoleInfo(MSGINvarchar2);
endPKG_COMM_UTILS;createorreplacepackagebodyPKG_COMM_UTILSis
procereconsoleInfo(MSGINvarchar2)is
begin
dbms_output.put_line(MSG);
end;
endPKG_COMM_UTILS;
调用:
declare
begin
PKG_COMM_UTILS.consoleInfo('HelloWorld!');
end;
输出:
Hello World !
不懂可以再问。
10. SQL将函数封装到存储过程,如何实现
思维混乱。没有逻辑。做事要一件件来。不能这样稀里糊涂。
第一步:创建函数:
Create function NeterCenter_ChangeChinese
(
@str nvarchar(4000)
)
returns nvarchar(4000)
as
begin
declare @word nchar(1),@PY nvarchar(4000)
set @PY=''
while len(@str)>0
begin
set @word=left(@str,1)
--如果非汉字字符,返回原字符
set @PY=@PY+(case when unicode(@word) between 19968 and 19968+20901
then (
select top 1 PY
from
(
select 'A' as PY,N'骜' as word
union all select 'B',N'簿'
union all select 'C',N'错'
union all select 'D',N'鵽'
union all select 'E',N'樲'
union all select 'F',N'鳆'
union all select 'G',N'腂'
union all select 'H',N'夻'
union all select 'J',N'攈'
union all select 'K',N'穒'
union all select 'L',N'鱳'
union all select 'M',N'旀'
union all select 'N',N'桛'
union all select 'O',N'沤'
union all select 'P',N'曝'
union all select 'Q',N'囕'
union all select 'R',N'鶸'
union all select 'S',N'蜶'
union all select 'T',N'箨'
union all select 'W',N'鹜'
union all select 'X',N'鑂'
union all select 'Y',N'韵'
union all select 'Z',N'咗'
) T
where word>=@word collate Chinese_PRC_CS_AS_KS_WS
order by PY ASC
)
else @word
end)
set @str=right(@str,len(@str)-1)
end
return @PY
end
GO
--第二步:创建存储过程,调用函数:
create PROCEDURE sp_GetIt
@str varchar(3000)--参数
AS
select dbo.NeterCenter_ChangeChinese(@str) OutStr
--第三步,使用过程
sp_GetIt '我是中国人'
master是系统数据库,不要将你自己写的脚步放到该库,自己创建数据库。