C#各种辅助类收集(CSharpCommonHelper)

摘要:
Get(stringkey,int?int.TryParse(s,outv)){log.WarnForma

1.IOHelper(FileHelper)

2.XmlHelper

3.DBHelper

4.IISHelper

5.OSHelper(WindowsHelper)

6.SqlserverHelper

C#各种辅助类收集(CSharpCommonHelper)第1张C#各种辅助类收集(CSharpCommonHelper)第2张View Code
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.IO;

namespaceWindowsDeploy.Helpers
{
    public static classSqlServerHelper
    {
        public static int ExcuteNonQuery(string conStr, stringsqlStr)
        {
            SqlConnection con = newSqlConnection(conStr);
            try{
                con.Open();
                SqlCommand cmd = newSqlCommand(sqlStr, con);
                cmd.CommandType =CommandType.Text;
                returncmd.ExecuteNonQuery();
            }
            catch(Exception ex)
            {
                return -1;
            }
            finally{
                con.Close();
            }
        }

        public static int ExcuteNonQuery(string conStr, string sqlStr, CommandType type, paramsSqlParameter[] param)
        {
            SqlConnection con = newSqlConnection(conStr);
            try{
                con.Open();
                SqlCommand cmd = newSqlCommand(sqlStr, con);
                cmd.CommandType =type;
                foreach (SqlParameter p inparam)
                {
                    cmd.Parameters.Add(p);
                }
                returncmd.ExecuteNonQuery();
            }
            catch(Exception ex)
            {
                return -1;
            }
            finally{
                con.Close();
            }
        }

        /// <summary>
        ///还原数据库文件
        /// </summary>
        /// <param name="filePath">数据库备份文件名称路径</param>
        /// <param name="databaseName">需要还原的数据库名称</param>
        /// <param name="conStr">数据库连接</param>
        /// <returns></returns>
        public static bool RestoreDataBase(string filePath, string databaseName, stringconStr)
        {
            string fileName = filePath.Split('\\')[filePath.Split('\\').Length-1];
            string basePath=filePath.Replace(fileName,"");
            SqlConnection conn = newSqlConnection(conStr);
            SqlCommand command = null;
            try{
                string restoreStr = string.Empty;
                string getLogicFileName = string.Format("restore   filelistonly   from   disk    ='{0}'", filePath);
                DataSet ds = newDataSet();
                SqlDataAdapter da = newSqlDataAdapter(getLogicFileName, conn);
                da.Fill(ds);
                if (ds.Tables == null || ds.Tables[0].Rows.Count == 0) return false;

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    //查找数据库逻辑文件名称
                    if ("D".Equals(dr["Type"].ToString()))
                        restoreStr += string.Format("move '{0}' to '{1}',", dr["LogicalName"].ToString(), Path.Combine(basePath, databaseName) + "_Data.mdf");
                    //查找数据库日志文件名称
                    else if ("L".Equals(dr["Type"].ToString()))
                        restoreStr += string.Format("move '{0}' to '{1}',", dr["LogicalName"].ToString(), Path.Combine(basePath, databaseName) + "_Log.ldf");
                }
                if (string.IsNullOrEmpty(restoreStr))
                    restoreStr = string.Format("restore   database  {0}   from   disk    ='{1}'", databaseName, Path.Combine(basePath, fileName));
                else{
                    restoreStr = string.Format("restore   database  {0}   from   disk    ='{1}' with replace,", databaseName, Path.Combine(basePath, fileName)) + restoreStr.TrimEnd(',');
                }
                command = newSqlCommand(restoreStr, conn);
                conn.Open();
                command.ExecuteNonQuery();
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
            finally{
                conn.Close();
            }
        }
    }
}

7.AppConfigHelper

C#各种辅助类收集(CSharpCommonHelper)第3张C#各种辅助类收集(CSharpCommonHelper)第4张View Code
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Configuration;
usingSystem.Text;
usinglog4net;

namespacext {
    public classAppConfigHelper {
        private static ILog log = LogManager.GetLogger(typeof(AppConfigHelper));
        
        public static int? Get(string key, int?defaultValue) {
            string s =ConfigurationManager.AppSettings[key];
            if (string.IsNullOrEmpty(s)) {
                log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key);
            } else{
                intv;
                if (!int.TryParse(s, outv)) {
                    log.WarnFormat("The key {1} is not properly configured, using default: {0}", defaultValue, key);
                } else{
                    defaultValue =v;
                    log.InfoFormat("Get: {0}={1}", key, defaultValue);
                }
            }
            returndefaultValue;
        }
        
        public static bool? Get(string key, bool?defaultValue) {
            string s =ConfigurationManager.AppSettings[key];
            if (string.IsNullOrEmpty(s)) {
                log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key);
            } else{
                boolv;
                if (!bool.TryParse(s, outv)) {
                    log.WarnFormat("The key {1} is not properly configured, using default: {0}", defaultValue, key);
                } else{
                    defaultValue =v;
                    log.InfoFormat("Get: {0}={1}", key, defaultValue);
                }
            }
            returndefaultValue;
        }

        public static T GetEnum<T>(stringkey, T defaultValue) {
            string s =ConfigurationManager.AppSettings[key];
            if (string.IsNullOrEmpty(s)) {
                log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key);
                returndefaultValue;
            } else{
                T v = (T) Enum.Parse(typeof(T), s);
                log.InfoFormat("Get: {0}={1}", key, defaultValue);
                returnv;
            }
        }
        
        public static string Get(string key, stringdefaultValue) {
            string s =ConfigurationManager.AppSettings[key];
            if (string.IsNullOrEmpty(s)) {
                log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key);
                returndefaultValue;
            }
            log.InfoFormat("Get: {0}={1}", key, s);
            returns;
        }

        public static DateTime Get(stringkey, DateTime defaultValue) {
            string s =ConfigurationManager.AppSettings[key];
            if (string.IsNullOrEmpty(s)) {
                log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key);
                returndefaultValue;
            }
            DateTime result;
            if(!DateTime.TryParse(s, outresult)) {
                log.WarnFormat("The key {0} is not properly configured, using default: {1} isntead of {3}", key, defaultValue, s);
                returndefaultValue;
            }
            log.InfoFormat("Get: {0}={1}", key, s);
            returnresult;
        }

        public static TimeSpan Get(stringkey, TimeSpan defaultValue) {
            string s =ConfigurationManager.AppSettings[key];
            if (string.IsNullOrEmpty(s)) {
                log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key);
                returndefaultValue;
            }
            TimeSpan result;
            if (!TimeSpan.TryParse(s, outresult)) {
                log.WarnFormat("The key {0} is not properly configured, using default: {1} isntead of {3}", key, defaultValue, s);
                returndefaultValue;
            }
            log.InfoFormat("Get: {0}={1}", key, s);
            returnresult;
        }
    }

陆续更新中。。。

下载地址: https://files.cnblogs.com/8090sns/DotNet.Utilities.rar

免责声明:文章转载自《C#各种辅助类收集(CSharpCommonHelper)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇访问 Confluence 6 的计划任务配置SDK更新下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Java String.split()函数分隔回车注意事项

作者:Sun1956原文:https://blog.csdn.net/sun1956/article/details/45096117 --------------------- 我们在Java中如果用到处理要分隔字符串,不免会用到String的split(String regex)函数,这时候我们要注意的。 如果想以回车来分隔字符串,很多同学都会这样做。...

G代码简单解释

公司的切割机用的是梅塞尔的等离子切割机,技术部在下发套料图的时候,会将指令文件做简化处理,形式如下: %N4G92X0Y0N6F5000N8G71N10G91N12G00X561.243Y1277.726N14M11N16M09N18G01X75.071Y160.966N20G01X-44.119Y75.746N22G01X86.386Y14.882N24...

app微信支付-java服务端接口 支付-查询-退款

个人不怎么看得懂微信的文档,看了很多前辈的写法,终于调通了,在这里做一下记录。 首先来定义各种处理类(微信支付不需要特殊jar包,很多处理需要自己封装,当然也可以自己写完打个jar包) 参数要用jdom解析   自行导入jar包    或者在maven pom.xml中导入  <dependency> <groupId>org...

Dubbo学习笔记12:使用Dubbo中需要注意的一些事情

指定方法异步调用 前面我们讲解了通过设置ReferenceConfig的setAsync()方法来让整个接口里的所有方法变为异步调用,那么如何指定某些方法为异步调用呢?下面讲解下如何正确地设置默写方法为异步调用。 假如你只需要设置接口里的方法sayHello为异步调用,那么可以使用下面方式: final List<MethodConfig> a...

c# 通过程序修改hosts文件

1 根据ip替换 var OSInfo =Environment.OSVersion; string pathpart = "hosts"; if (OSInfo.Platform ==PlatformID.Win32NT) { //is windows NT pathpart = "system32\drivers\etc\hosts";...

Java引用总结--StrongReference、SoftReference、WeakReference、PhantomReference

Java引用总结--StrongReference、SoftReference、WeakReference、PhantomReference   1 Java引用介绍   Java从1.2版本开始引入了4种引用,这4种引用的级别由高到低依次为:    强引用  >  软引用  >  弱引用  >  虚引用 ⑴强引用(StrongRefe...