• 【2016-11-15】【坚持学习】【Day26】【通用的SQLHelper】


    今天看DevDemo源码时候看到一个写得很好的SQLHelper

    代码

      public class SqlHelper<T, Command>
            where T : DbConnection
            where Command : DbCommand, new() {
            public object ReadValue(T connection, string selectQuery, params DbParameter[] pars) {
                using(var sql = CreateCommand(selectQuery, connection)) {
                    sql.CommandTimeout = 1000;
                    if(pars != null)
                        sql.Parameters.AddRange(pars);
                    try {
                        return CheckDbNull(sql.ExecuteScalar());
                    }
                    catch { return null; }
                }
            }
            object CheckDbNull(object value) {
                if(value == null) return null;
                if(Object.ReferenceEquals(value, DBNull.Value)) return null;
                return value;
            }
            static DbCommand CreateCommand(string selectQuery, T connection) {
                return new Command() { CommandText = selectQuery, Connection = connection };
            }
            public List<object[]> ReadValues(T connection, string selectQuery, params DbParameter[] pars) {
                List<object[]> res = new List<object[]>();
                try {
                    using(var sql = CreateCommand(selectQuery, connection)) {
                        sql.CommandTimeout = 5000;
                        if(pars != null) sql.Parameters.AddRange(pars);
                        using(DbDataReader reader = sql.ExecuteReader()) {
                            if(!reader.HasRows) return res;
                            while(reader.Read()) {
                                object[] values = new object[reader.FieldCount];
                                reader.GetValues(values);
                                res.Add(values);
                            }
                        }
                    }
                }
                catch { }
                return res;
            }
            public string GetString(object val) {
                if(val == DBNull.Value || val == null) return string.Empty;
                return val.ToString();
            }
            public DateTime GetDateInv(object val) {
                if(val == DBNull.Value || val == null) return DateTime.MinValue;
                return DateTime.ParseExact(val.ToString(), "d/M/yyyy", DateTimeFormatInfo.InvariantInfo);
            }
            public int GetInt(object val) {
                if(val == DBNull.Value || val == null) return 0;
                return Convert.ToInt32(val);
            }
            public DateTime GetDate(object value) {
                if(value == null || value == DBNull.Value) return DateTime.MinValue;
                if(value is DateTime) return (DateTime)value;
                return GetDateInv(value);
            }
            public decimal GetDecimal(object value) {
                if(value == null || value == DBNull.Value) return 0;
                return (decimal)Convert.ChangeType(value, typeof(decimal));
            }
            public Guid GetGuid(object value) {
                if(value == null || value == DBNull.Value) return Guid.Empty;
                return new Guid(value.ToString());
            }
            public bool GetBool(object value) {
                if(value == null || value == DBNull.Value) return false;
                if(value is bool) return (bool)value;
                if(value is int) return (int)value == 1;
                return false;
            }
        }
       using (var connection = new OleDbConnection(connectionString))
                {
                    connection.Open();
                    //new OleDbCommand("delete * from sales", connection).ExecuteNonQuery(); //FOR TEST PURPOSES
                    var helper = new SqlHelper<OleDbConnection, OleDbCommand>();
                    DateTime minDate = helper.GetDate(helper.ReadValue(connection, "select min(sale_date) from sales"));
                    DateTime maxDate = helper.GetDate(helper.ReadValue(connection, "select max(sale_date) from sales"));
                    this.maxId = helper.GetInt(helper.ReadValue(connection, "select max(id) from sales"));
                    DateTime startDate = minRequiredDate;
                    if (minDate > startDate && maxDate != DateTime.MinValue)
                    {
                        startDate = maxDate.AddDays(1);
                    }
                    if (startDate > DateTime.Today.AddDays(2))
                        return true;
                    double daysCount = currentDate.Subtract(startDate).TotalDays;
                    RaiseStart();
                    try { Generate(connection, startDate, (int)daysCount); }
                    finally { RaiseComplete(); }
                    connection.Close();
                }

    作者:zscmj
    出处:http://www.cnblogs.com/zscmj/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    windows服务安装命令
    vue开发
    Quorum(NRW)算法机制简介
    SideCar模式
    Python抓取数据的几种方式
    Ms SqlServer索引的选择
    cmd添加环境变量
    好用工具网站
    Aufofac生命周期
    EF初次加载优化
  • 原文地址:https://www.cnblogs.com/zscmj/p/6067307.html
Copyright © 2020-2023  润新知