存一个工作中常用的类

CommonHelper.cs -- 数据库查询 及 HTTP 请求类

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace GetAddressService
{
    public class CommonHelper
    {
        public static string Con { get; set; }

        
        public static DataTable FillTable(string SQLString, string conStr=null)
        {
            if (string.IsNullOrEmpty(conStr))
                conStr = Con;
            using (OracleConnection connection = new OracleConnection(conStr))
            {
                DataTable dt = new DataTable();
                try
                {
                    connection.Open();
                    OracleDataAdapter command = new OracleDataAdapter(SQLString, connection);
                    command.Fill(dt);
                }
                catch (System.Data.OracleClient.OracleException ex)
                {
                    throw new Exception(ex.Message);

                }
                return dt;
            }
        }

        public static int ExecuteSql(string SQLString, string conStr=null)
        {
            if (string.IsNullOrEmpty(conStr))
                conStr = Con;
            using (OracleConnection connection = new OracleConnection(conStr))
            {
                using (OracleCommand cmd = new OracleCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        int rows = cmd.ExecuteNonQuery();
                        return rows;
                    }
                    catch (System.Data.OracleClient.OracleException E)
                    {
                        throw new Exception(E.Message);
                    }
                }
            }
        }

        public static string Http(Uri uri, byte[] data = null)
        {
            string rtnVal = "";
            int tryTimes = 0;
        again:
            tryTimes++;
            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(uri);
                webRequest.Method = "GET";
                webRequest.ContentType = "application/x-www-form-urlencoded";
                if (data != null)
                {
                    webRequest.Method = "POST";
                    webRequest.ContentLength = data.Length;
                    Stream inputStream = webRequest.GetRequestStream();
                    inputStream.Write(data, 0, data.Length);
                    inputStream.Close();
                    inputStream.Dispose();
                    inputStream = null;
                }
                HttpWebResponse webResp = (HttpWebResponse)webRequest.GetResponse();
                using (Stream receiveStream = webResp.GetResponseStream())
                {
                    using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
                    {
                        rtnVal = readStream.ReadToEnd();
                    }
                }
            }
            catch (Exception)
            {
                if (tryTimes < 1) goto again;
            }
            return rtnVal;
        }
    }
} 

//即获取到的数据库连接字符串在 app.Config 配置如下 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="constr"  value="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.1)(PORT=1521)))(CONNECT_DATA=(service_name =acid)));Persist Security Info=True;User ID=wq; Password=123;"/>
    <add key="ConnectionString" value="Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.1)(PORT=1521)))(CONNECT_DATA=(service_name =ney)));Persist Security Info=True;User ID=sa;Password=123;"/>
  </appSettings>
</configuration>

取constr连接字符串如下

CommonHelper.Con = System.Configuration.ConfigurationManager.AppSettings["constr"];

注:这里需要引用程序集 

存一个打日志的公用类(注:这个是从DOS.ORM中独立出来的)

LogHelper.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;

namespace Dos.Common
{
    /// <summary>
    /// 日志帮助类。AppSettings节点可以配置Dos.LogHelper.Debug=0或Dos.LogHelper.Error=0来关闭日志记录。
    /// 如果不传入path参数,默认是在~/Log/下生成日志文件,也可以在AppSettings节点配置Dos.LogHelper.Path来设置默认日志文件路径,格式:D:\File\Log\。
    /// </summary>
    public class LogHelper
    {
        private static readonly object Olock = new object();
        private enum LogHelperType
        {
            debug, error
        }
        /// <summary>
        /// 记录调试日志
        /// </summary>
        /// <Param name="content">内容。如需换行可使用:
</Param>
        /// <Param name="filePrefixName"></Param>
        /// <Param name="path">格式:D:\File\Logs\</Param>
        public static void Debug(string content, string filePrefixName = null, string path = null)
        {
            Write(LogHelperType.debug, content, filePrefixName, path);
        }
        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <Param name="content">内容。如需换行可使用:
</Param>
        /// <Param name="filePrefixName"></Param>
        /// <Param name="path">格式:D:\File\Logs\</Param>
        public static void Error(string content, string filePrefixName = null, string path = null)
        {
            Write(LogHelperType.error, content, filePrefixName, path);
        }
        /// <summary>
        /// filePrefixName是文件名前缀,最好用中文,方便在程序Logs文件下查看。
        /// </summary>
        /// <Param name="content">内容。如需换行可使用:
</Param>
        /// <Param name="filePrefixName"></Param>
        /// <Param name="path"></Param>
        /// <Param name="logtype"></Param>
        private static void Write(LogHelperType logtype, string content, string filePrefixName = null, string path = null)
        {
            lock (Olock)
            {
                try
                {
                    if (logtype == LogHelperType.debug)
                    {
                        var dosDebug = ConfigurationManager.AppSettings["Dos.LogHelper.Debug"];
                        if (dosDebug != null && dosDebug != "1")
                        {
                            return;
                        }
                    }
                    else
                    {
                        var dosError = ConfigurationManager.AppSettings["Dos.LogHelper.Error"];
                        if (dosError != null && dosError != "1")
                        {
                            return;
                        }
                    }
                    #region 日志文件
                    var fileName = filePrefixName + DateTime.Now.ToString("yyyyMMdd") + logtype.ToString() + ".txt";
                    if (string.IsNullOrWhiteSpace(path))
                    {
                        var dosPath = ConfigurationManager.AppSettings["Dos.LogHelper.Path"];
                        if (string.IsNullOrWhiteSpace(dosPath))
                        {
                            path = AppDomain.CurrentDomain.BaseDirectory + "\Logs\" + fileName;
                        }
                        else
                        {
                            path = dosPath + fileName;
                        }
                    }
                    else
                    {
                        path += fileName;
                    }
                    var di = new DirectoryInfo(path.Replace(fileName, ""));
                    if (!di.Exists)
                    {
                        di.Create();
                    }
                    //判断文件大小,需要新开文件
                    using (var fs = new FileStream(path, FileMode.Append, FileAccess.Write))
                    {
                        var sw = new StreamWriter(fs);
                        sw.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        sw.WriteLine();
                        sw.Write(content);
                        sw.WriteLine();
                        sw.Write("-----------------------------------------------------------------------------");
                        sw.WriteLine();
                        sw.Flush();
                        sw.Close();
                    }
                    #endregion
                }
                catch
                {
                }
            }
        }
    }
}
LogHelper.cs

例如需要打印异常日志

try{
   //处理一些事情
 }
catch (Exception ex)
            {
                Dos.Common.LogHelper.Debug(ex.Message);
            }

如果不传入path参数,默认是在~/Log/下生成日志文件

原文地址:https://www.cnblogs.com/zhibu/p/7248405.html