.cs文件封装成dll文件的步骤及dll文件的理解

个人总结:

.dll文件实际是.cs文件方法的封装【封装起来别人就看不到了,但是可以通过Reflector软件打开,看代码】

步骤:

1.新建DbHelp.cs文件

 //数据库链接字符串 添加程序集【System.Configuration】+添加命名控件【using System.Configuration】
        private static readonly string connectionString = ConfigurationManager.ConnectionStrings["SqlConn"].ConnectionString.ToString();

        private static readonly string providerName = "System.Data.SqlClient";
        
        #region GetConnection 用于获取连接数据库的 connection 对象
        /// <summary>
        /// GetConnection 用于获取连接数据库的 connection 对象
        /// </summary>
        /// <returns></returns>
        /// //添加命名控件using System.Data.Common;
        private static DbConnection GetConnection()
        {
            DbProviderFactory _factory = DbProviderFactories.GetFactory(providerName);
            DbConnection connection = _factory.CreateConnection();
            connection.ConnectionString = connectionString;
            return connection;
        }
        #endregion

        #region GetCommand 获取命令参数 command 对象
        /// <summary>
        /// GetCommand 获取命令参数 command 对象
        /// </summary>
        /// <param name="commandText"></param>
        /// <param name="commandType"></param>
        /// <param name="connection"></param>
        /// <returns></returns>
        private static DbCommand GetCommand(string commandText, CommandType commandType, DbConnection connection)
        {
            DbCommand command = connection.CreateCommand();
            command.CommandType = commandType;
            command.CommandText = commandText;
            return command;
        }
        #endregion

        #region GetCommand方法重载
        /// <summary>
        /// GetCommand方法重载
        /// </summary>
        /// <param name="commadText">sal语句</param>
        /// <param name="connection">数据库链接</param>
        /// <returns></returns>
        private static DbCommand GetCommand(string commadText, DbConnection connection)
        {
            DbCommand command = connection.CreateCommand();
            command.CommandText = commadText;
            command.CommandType = CommandType.Text;
            return command;
        }
        #endregion

        #region GetParameter 用于为命令设置参数
        /// <summary>
        /// GetParameter 用于为命令设置参数
        /// </summary>
        /// <param name="paramName"></param>
        /// <param name="paramValue"></param>
        /// <param name="command"></param>
        /// <returns></returns>
        private static DbParameter GetParameter(string paramName, object paramValue, DbCommand command)
        {
            DbParameter parameter = command.CreateParameter();
            parameter.ParameterName = paramName;
            parameter.Value = paramValue;
            return parameter;
        }
        #endregion

        #region ExecuteNonQueryProc执行无参的存储过程
        /// <summary>
        /// 执行无参的存储过程
        /// </summary>
        /// <param name="sqlCommand">存储过程名</param>
        /// <returns></returns>
        public static int ExecuteNonQueryProc(string sqlCommand)
        {
            int result = 0;
            using (DbConnection connection = GetConnection())
            {
                DbCommand command = GetCommand(sqlCommand, CommandType.StoredProcedure, connection);
                connection.Open();
                result = command.ExecuteNonQuery();
                command.Parameters.Clear();
            }
            return result;
        }
        #endregion

以上只给出了部分方法的代码。

2.再用VS把cs文件封装成dll文件 方法参照:http://blog.csdn.net/qq_33903684/article/details/54583323

3.把DbHelp.cs文件删除,把生成的DbHelp.dll文件拷贝到DAL-->Bin-->Debug里面,在项目DAL层添加应用,找到DbHelp.dll文件,在DAL层就可以直接使用了,例如:

 

public static int Gsdf()
        {
            string sql = "insert into Flower(name) values(@name)";
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("@name","玫瑰");
            return DbHelp.ExecuteNonQuery(sql,dic);
        }

 

 

 

 

 

 



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值