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

1.IOHelper(FileHelper)

2.XmlHelper

3.DBHelper

4.IISHelper

5.OSHelper(WindowsHelper)

6.SqlserverHelper

View Code
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data;

using System.Data.SqlClient;

using System.IO;



namespace WindowsDeploy.Helpers

{

    public static class SqlServerHelper

    {

        public static int ExcuteNonQuery(string conStr, string sqlStr)

        {

            SqlConnection con = new SqlConnection(conStr);

            try

            {

                con.Open();

                SqlCommand cmd = new SqlCommand(sqlStr, con);

                cmd.CommandType = CommandType.Text;

                return cmd.ExecuteNonQuery();

            }

            catch (Exception ex)

            {

                return -1;

            }

            finally

            {

                con.Close();

            }

        }



        public static int ExcuteNonQuery(string conStr, string sqlStr, CommandType type, params SqlParameter[] param)

        {

            SqlConnection con = new SqlConnection(conStr);

            try

            {

                con.Open();

                SqlCommand cmd = new SqlCommand(sqlStr, con);

                cmd.CommandType = type;

                foreach (SqlParameter p in param)

                {

                    cmd.Parameters.Add(p);

                }

                return cmd.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, string conStr)

        {

            string fileName = filePath.Split('\\')[filePath.Split('\\').Length-1];

            string basePath=filePath.Replace(fileName,"");

            SqlConnection conn = new SqlConnection(conStr);

            SqlCommand command = null;

            try

            {

                string restoreStr = string.Empty;

                string getLogicFileName = string.Format("restore   filelistonly   from   disk    ='{0}'", filePath);

                DataSet ds = new DataSet();

                SqlDataAdapter da = new SqlDataAdapter(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 = new SqlCommand(restoreStr, conn);

                conn.Open();

                command.ExecuteNonQuery();

                return true;

            }

            catch (Exception ex)

            {

                return false;

            }

            finally

            {

                conn.Close();

            }

        }

    }

}

 7.AppConfigHelper

View Code
using System;

using System.Collections.Generic;

using System.Configuration;

using System.Text;

using log4net;



namespace xt {

    public class AppConfigHelper {

        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 {

                int v;

                if (!int.TryParse(s, out v)) {

                    log.WarnFormat("The key {1} is not properly configured, using default: {0}", defaultValue, key);

                } else {

                    defaultValue = v;

                    log.InfoFormat("Get: {0}={1}", key, defaultValue);

                }

            }

            return defaultValue;

        }

        

        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 {

                bool v;

                if (!bool.TryParse(s, out v)) {

                    log.WarnFormat("The key {1} is not properly configured, using default: {0}", defaultValue, key);

                } else {

                    defaultValue = v;

                    log.InfoFormat("Get: {0}={1}", key, defaultValue);

                }

            }

            return defaultValue;

        }



        public static T GetEnum<T>(string key, T defaultValue) {

            string s = ConfigurationManager.AppSettings[key];

            if (string.IsNullOrEmpty(s)) {

                log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key);

                return defaultValue;

            } else {

                T v = (T) Enum.Parse(typeof (T), s);

                log.InfoFormat("Get: {0}={1}", key, defaultValue);

                return v;

            }

        }

        

        public static string Get(string key, string defaultValue) {

            string s = ConfigurationManager.AppSettings[key];

            if (string.IsNullOrEmpty(s)) {

                log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key);

                return defaultValue;

            }

            log.InfoFormat("Get: {0}={1}", key, s);

            return s;

        }



        public static DateTime Get(string key, DateTime defaultValue) {

            string s = ConfigurationManager.AppSettings[key];

            if (string.IsNullOrEmpty(s)) {

                log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key);

                return defaultValue;

            }

            DateTime result;

            if(!DateTime.TryParse(s, out result)) {

                log.WarnFormat("The key {0} is not properly configured, using default: {1} isntead of {3}", key, defaultValue, s);

                return defaultValue;

            }

            log.InfoFormat("Get: {0}={1}", key, s);

            return result;

        }



        public static TimeSpan Get(string key, TimeSpan defaultValue) {

            string s = ConfigurationManager.AppSettings[key];

            if (string.IsNullOrEmpty(s)) {

                log.WarnFormat("The key {1} is not configured, using default: {0}", defaultValue, key);

                return defaultValue;

            }

            TimeSpan result;

            if (!TimeSpan.TryParse(s, out result)) {

                log.WarnFormat("The key {0} is not properly configured, using default: {1} isntead of {3}", key, defaultValue, s);

                return defaultValue;

            }

            log.InfoFormat("Get: {0}={1}", key, s);

            return result;

        }

    }


 

陆续更新中。。。

 

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

你可能感兴趣的:(help)