zoukankan      html  css  js  c++  java
  • 【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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    面向对象第三单元博客作业
    面向对象编程第2次总结(电梯作业)
    面向对象编程第1次总结
    OOP 第四章博客总结
    OO 第三章总结
    OOP 第二章作业总结
    Java 设计模式 -- 代理模式
    ASID 与 MIPS 中 TLB 相关
    Java 锁机制总结
    OOP 第一章作业总结
  • 原文地址:https://www.cnblogs.com/zscmj/p/6067307.html
Copyright © 2011-2022 走看看