Helper class for development
1. encapsulate config In the Object layer;
Example 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. for 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. CRMConfi GInstance. userName = System. configuration. configurationManager. deleetask[ "UserName"]; this. CRMConfigInstance. password = System. configuration. configurationManager. deleetask[ "Password"]; this. CRMConfigInstance. domain = System. configuration. configurationManager. deleetask[ "Domain"]; this. CRMConfigInstance. connectionString = System. configuration. configurationManager. connectionStrings ["MZJCRM"]. co NnectionString;} catch (Exception ex) {Ex = ex;} finally {if (Ex! = Null) {// predictionhelper. 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 Version
2. Application version 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 XmlDocum Ent (); 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"); // datasourcesetaskxmlnode datasourcesetask= doc. documentElement. selectSingleNode ("DataSourceSettings"); XmlNode SourceConnectionString = DataSourceSettings. selectSingleNode (@ "ConnectionString"); this. CRMConfigInstance. userName = UserName. innerText; thi S. 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) {predictionhelper. 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 ;}}}Application version
Ii. SqlHeleper class
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. indente D; 28 doc. save (writer); // convert 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 struct type, string plain text, SqlParameter [] cmdP Arms) 54 {55 56 // judge database connection status 57 if (conn. State! = ConnectionState. open) 58 conn. open (); 59 60 cmd. connection = conn; 61 cmd. commandText = plain text; 62 63 // determine whether transaction processing is required 64 if (trans! = Null) 65 cmd. Transaction = trans; 66 67 cmd. CommandType = partition type; 68 69 if (partition parms! = Null) 70 {71 foreach (SqlParameter parm in parallel parms) 72 cmd. Parameters. Add (parm); 73} 74} 75}SqlHelper