CommonHelper.cs -- 數據庫查詢 及 HTTP 請求類web
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"];
注:這裏須要引用程序集 app
存一個打日誌的公用類(注:這個是從DOS.ORM中獨立出來的)ide
LogHelper.csurl
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">內容。如需換行可以使用:\r\n</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">內容。如需換行可以使用:\r\n</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">內容。如需換行可以使用:\r\n</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 { } } } } }
例如須要打印異常日誌spa
try{ //處理一些事情 } catch (Exception ex) { Dos.Common.LogHelper.Debug(ex.Message); }
若是不傳入path參數,默認是在~/Log/下生成日誌文件debug