zoukankan      html  css  js  c++  java
  • 配置文件辅助类

    using Utility;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using DBHelper;
    
    namespace Test
    {
    
        //Config表的SQL脚本
    
        //CREATE TABLE [Config](
        //    [Name] [varchar](800) NOT NULL,
        //    [Value] [nvarchar](Max) NULL,
        // CONSTRAINT [PK_Config] PRIMARY KEY CLUSTERED 
        //(
        //    [Name] ASC
        //)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
        //) ON [PRIMARY]
        //GO
        public static class Config
        {
            /// <summary>
            /// 用法:var createTime = Config.GetValue(ConfigString.CreateTime, new DateTime(2000, 1, 1));
            /// </summary> 
            public static T GetValue<T>(string key, T defaultVaule)
            {
                T ret = (T)Convert.ChangeType(defaultVaule, typeof(T));
                try
                {
                    string dbValue = GetValue(key);
                    ret = (T)Convert.ChangeType(dbValue, typeof(T));
                }
                catch (Exception ex)
                {
                    ex.SaveLog();
                }
                return ret;
            }
    
            /// <summary>
            /// 用法:Config.SetValue(ConfigString.CreateTime, DateTime.Now); 
            /// <returns></returns>
            public static bool SetValue<T>(string key, T value)
            {
                bool result = true;
                try
                {
                    if (Exists(key))
                    {
                        result = Update(key, value);
                    }
                    else
                    {
                        result = Add(key, value);
                    }
                }
                catch (Exception ex)
                {
                    ex.SaveLog();
                    result = false;
                }
                return result;
            }
    
            #region db操作
            private const string TABLE_NAME = "Config";
    
            private static bool Exists(string Name)
            {
                string sql = string.Format("select count(1) from {0} where Name=@Name", TABLE_NAME);
                StringBuilder strSql = new StringBuilder();
                strSql.AppendFormat("select count(1) from {0}", TABLE_NAME);
                strSql.Append(" where Name= @Name");
                SqlParameter[] parameters = {
                        new SqlParameter("@Name", SqlDbType.VarChar ,800)
                    };
                parameters[0].Value = Name;
                return DbHelperSQL.Exists(strSql.ToString(), parameters);
            }
    
            private static bool Add<T>(string Name, T Value)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.AppendFormat("insert into {0}(Name,Value) values (@Name,@Value)", TABLE_NAME);
                SqlParameter[] parameters = {
                        new SqlParameter("@Name", SqlDbType.VarChar,50),
                        new SqlParameter("@Value",SqlDbType.NVarChar)};
                parameters[0].Value = Name;
                parameters[1].Value = Value.ToString();
    
                //这里加了验证. 防止有重复时插入不进
                try
                {
                    return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
                }
                catch (Exception ex)
                {
                    ex.SaveLog();
                    return false;
                }
            }
    
            private static bool Update<T>(string Name, T Value)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.AppendFormat("update {0} set Value=@Value where Name=@Name", TABLE_NAME);
                SqlParameter[] parameters = {
                        new SqlParameter("@Value",SqlDbType.NVarChar),
                        new SqlParameter("@Name", SqlDbType.VarChar,800)};
                parameters[0].Value = Value;
                parameters[1].Value = Name;
    
                return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
            }
    
            private static string GetValue(string Name)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.AppendFormat("select Value from {0} where Name=@Name", TABLE_NAME);
                SqlParameter[] parameters = {
                        new SqlParameter("@Name", SqlDbType.VarChar,800)};
                parameters[0].Value = Name;
                object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
                return obj == null ? string.Empty : obj.ToString();
            }
    
            #endregion
        }
        public class ConfigString
        {
            public const string CreateTime = "CreateTime";
        }
    }
  • 相关阅读:
    从CentOS7默认安装的/home中转移空间到根目录/
    Linux下MySQL默认对表名区分大小写
    maven获取最新的快照版本
    使用javaMail发送邮件
    Jenkins配置邮件通知(四)
    webhook实现提交代码自动触发Jenkins构建任务(三)
    Jenkins构建完成后自动部署到指定服务器(二)
    搭建Jenkins从gitlab获取maven项目持续集成(一)
    awt frame
    什么是json
  • 原文地址:https://www.cnblogs.com/linmilove/p/9408076.html
Copyright © 2011-2022 走看看