程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 便於開發的Helper類,便於開發Helper類

便於開發的Helper類,便於開發Helper類

編輯:C#入門知識

便於開發的Helper類,便於開發Helper類


一、將config封裝實體層;

例子config:

<?xml version="1.0" encoding="utf-8" ?>
<Settings>
  <CRMSettings>
    <UserName>administrator</UserName>
    <Password>Sh123456</Password>
    <Domain>SSO.COM</Domain>
    <ConnectionString>Data Source=.;Initial Catalog=XHMZJ_MSCRM;User ID=sa;password=Sh123456</ConnectionString>
  </CRMSettings>
  <DataSourceSettings>
    <ConnectionString>Data Source=.;Initial Catalog=XH_YWXT;User ID=sa;password=Sh123456</ConnectionString>
  </DataSourceSettings>
</Settings>


 1.對於web版本的web.config。

public class ConfigHelper { private static ConfigHelper _instance = null; public static ConfigHelper Instance { get { if (_instance == null) _instance = new ConfigHelper(); return _instance; } } public ConfigHelper() { this.CRMConfigInstance = new CRMConfig(); Exception Ex = null; try { this.CRMConfigInstance.ServerURL = System.Configuration.ConfigurationManager.AppSettings["WebUrl"]; this.CRMConfigInstance.UserName = System.Configuration.ConfigurationManager.AppSettings["UserName"]; this.CRMConfigInstance.Password = System.Configuration.ConfigurationManager.AppSettings["Password"]; this.CRMConfigInstance.Domain = System.Configuration.ConfigurationManager.AppSettings["Domain"]; this.CRMConfigInstance.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MZJCRM"].ConnectionString; } catch (Exception ex) { Ex = ex; } finally { if (Ex != null) { //ExceptionHelper.WriteLog(Ex); } } } public CRMConfig CRMConfigInstance = null; } public class CRMConfig { private string _ConnectionString; public string ConnectionString { set { _ConnectionString = value; } get { return _ConnectionString; } } #region CRMConfig private string _serverURL; public string ServerURL { set { _serverURL = value; } get { return _serverURL; } } private string _userName; public string UserName { set { _userName = value; } get { return _userName; } } private string _password; public string Password { set { _password = value; } get { return _password; } } private string _domain; public string Domain { set { _domain = value; } get { return _domain; } } #endregion } Web版本

 2.應用程序版本的config。

public class ConfigHelper { private static ConfigHelper _instance = null; public static ConfigHelper Instance { get { if (_instance == null) _instance = new ConfigHelper(); return _instance; } } const string configFileName = "CRMConfig.xml"; protected XmlDocument doc; public ConfigHelper() { this.CRMConfigInstance = new CRMConfig(); this.SourceConfigInstance = new SourceConfig(); doc = new XmlDocument(); Exception Ex = null; try { doc.Load(this.FileFullPath); XmlNode Settings = doc.DocumentElement.SelectSingleNode("Settings"); //CRMSettings XmlNode CrmSettings = doc.DocumentElement.SelectSingleNode("CRMSettings"); XmlNode WebUrl = CrmSettings.SelectSingleNode(@"WebUrl"); XmlNode UserName = CrmSettings.SelectSingleNode(@"UserName"); XmlNode Password = CrmSettings.SelectSingleNode(@"Password"); XmlNode Domain = CrmSettings.SelectSingleNode(@"Domain"); XmlNode CRMConnectionString = CrmSettings.SelectSingleNode(@"ConnectionString"); //DataSourceSettings XmlNode DataSourceSettings = doc.DocumentElement.SelectSingleNode("DataSourceSettings"); XmlNode SourceConnectionString = DataSourceSettings.SelectSingleNode(@"ConnectionString"); this.CRMConfigInstance.UserName = UserName.InnerText; this.CRMConfigInstance.Password = Password.InnerText; this.CRMConfigInstance.Domain = Domain.InnerText; this.CRMConfigInstance.ServerURL = WebUrl.InnerText; this.CRMConfigInstance.ConnectionString = CRMConnectionString.InnerText; this.SourceConfigInstance.ConnectionString = SourceConnectionString.InnerText; } catch(Exception ex) { Ex = ex; } finally { if(Ex != null) { ExceptionHelper.WriteLog(Ex); } } } private string _filePath; public string FilePath { set { this._filePath = value; } get { return System.Windows.Forms.Application.StartupPath; } } /// <summary> /// full path /// </summary> public string FileFullPath { get { return System.IO.Path.Combine(this.FilePath, configFileName); } } public CRMConfig CRMConfigInstance = null; public SourceConfig SourceConfigInstance = null; } public class CRMConfig { private string _ConnectionString; public string ConnectionString { set { _ConnectionString = value; } get { return _ConnectionString; } } #region CRMConfig private string _serverURL; public string ServerURL { set { _serverURL = value; } get { return _serverURL; } } private string _userName; public string UserName { set { _userName = value; } get { return _userName; } } private string _password; public string Password { set { _password = value; } get { return _password; } } private string _domain; public string Domain { set { _domain = value; } get { return _domain; } } #endregion } public class SourceConfig { private string _ConnectionString; public string ConnectionString { set { _ConnectionString = value; } get { return _ConnectionString; } } } 應用程序版本

二、SqlHeleper類

1 public static class SQLHelper 2 { 3 public static DataTable GetDataTable(string sqlStr, params SqlParameter[] sqlParameters) 4 { 5 using (SqlConnection conn = new SqlConnection(ConfigHelper.Instance.CRMConfigInstance.ConnectionString)) 6 { 7 conn.Open(); 8 using (SqlCommand cmd = conn.CreateCommand()) 9 { 10 cmd.CommandText = sqlStr; 11 foreach (SqlParameter sqlParameter in sqlParameters) 12 { 13 cmd.Parameters.Add(sqlParameter); 14 } 15 DataSet dataSet = new DataSet(); 16 SqlDataAdapter adapter = new SqlDataAdapter(cmd); 17 adapter.Fill(dataSet); 18 return dataSet.Tables[0]; 19 } 20 } 21 } 22 23 public static string XMLDocumentToString(ref XmlDocument doc) 24 { 25 MemoryStream stream = new MemoryStream(); 26 XmlTextWriter writer = new XmlTextWriter(stream, null); 27 writer.Formatting = Formatting.Indented; 28 doc.Save(writer); //轉換 29 30 StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8); 31 stream.Position = 0; 32 string xmlString = sr.ReadToEnd(); 33 sr.Close(); 34 stream.Close(); 35 36 return xmlString; 37 } 38 39 public static int ExecuteNonQuerys(string SQL, params SqlParameter[] commandParameters) 40 { 41 SqlCommand cmd = new SqlCommand(); 42 43 using (SqlConnection connection = new SqlConnection(ConfigHelper.Instance.CRMConfigInstance.ConnectionString)) 44 { 45 PrepareCommand(cmd, connection, null, CommandType.Text, SQL, commandParameters); 46 int val = cmd.ExecuteNonQuery(); 47 48 cmd.Parameters.Clear(); 49 return val; 50 } 51 } 52 53 private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) 54 { 55 56 //判斷數據庫連接狀態 57 if (conn.State != ConnectionState.Open) 58 conn.Open(); 59 60 cmd.Connection = conn; 61 cmd.CommandText = cmdText; 62 63 //判斷是否需要事物處理 64 if (trans != null) 65 cmd.Transaction = trans; 66 67 cmd.CommandType = cmdType; 68 69 if (cmdParms != null) 70 { 71 foreach (SqlParameter parm in cmdParms) 72 cmd.Parameters.Add(parm); 73 } 74 } 75 } SqlHelper

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved