一、将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类

   public static class SQLHelper
{
public static DataTable GetDataTable(string sqlStr, params SqlParameter[] sqlParameters)
{
using (SqlConnection conn = new SqlConnection(ConfigHelper.Instance.CRMConfigInstance.ConnectionString))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sqlStr;
foreach (SqlParameter sqlParameter in sqlParameters)
{
cmd.Parameters.Add(sqlParameter);
}
DataSet dataSet = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dataSet);
return dataSet.Tables[];
}
}
} public static string XMLDocumentToString(ref XmlDocument doc)
{
MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, null);
writer.Formatting = Formatting.Indented;
doc.Save(writer); //转换 StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
stream.Position = ;
string xmlString = sr.ReadToEnd();
sr.Close();
stream.Close(); return xmlString;
} public static int ExecuteNonQuerys(string SQL, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand(); using (SqlConnection connection = new SqlConnection(ConfigHelper.Instance.CRMConfigInstance.ConnectionString))
{
PrepareCommand(cmd, connection, null, CommandType.Text, SQL, commandParameters);
int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear();
return val;
}
} private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{ //判断数据库连接状态
if (conn.State != ConnectionState.Open)
conn.Open(); cmd.Connection = conn;
cmd.CommandText = cmdText; //判断是否需要事物处理
if (trans != null)
cmd.Transaction = trans; cmd.CommandType = cmdType; if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}

SqlHelper

便于开发的Helper类的更多相关文章

  1. .NET中的加密算法总结(自定义加密Helper类续)

    1.1.1 摘要 相信许多人都使用过.NET提供的加密算法,而且在使用的过程我们必须了解每种加密算法的特点(对称或非对称,密钥长度和初始化向量等等).我也看到过很多人写过.NET中加密算法总结,但我发 ...

  2. 之前采用的是Helper类的方法重构时改用了扩展方法

    在手机端输入网址不方全,通常会将网址做成一个二维码,然后用手机扫一下就可以打开预览.我们每改一下样式,就在手机上点一下刷新或电脑上按一下F5,这在最初的时候,也不觉得有什么问题,因为拿到我手上的静态页 ...

  3. 开发H5程序或者小程序的时候,后端Web API项目在IISExpress调试中使用IP地址,便于开发调试

    在我们开发开发H5程序或者小程序的时候,有时候需要基于内置浏览器或者微信开发者工具进行测试,这个时候可以采用默认的localhost进行访问后端接口,一般来说没什么问题,如果我们需要通过USB基座方式 ...

  4. DbUtils是Apache出品一款简化JDBC开发的工具类

    DbUtils     - DbUtils是Apache出品一款简化JDBC开发的工具类     - 使用DbUtils可以让我们JDBC的开发更加简单     - DbUtils的使用:       ...

  5. MVC中使用HTML Helper类扩展HTML控件

    文章摘自:http://www.cnblogs.com/zhangziqiu/archive/2009/03/18/1415005.html MVC在view页面,经常需要用到很多封装好的HTML控件 ...

  6. 一点ASP.NET MVC Html.Helper类的方法

    一点ASP.NET MVC Html.Helper类 这里就只写一个Html.ActionLink()和Html.DropdownList(). Html.ActionLink()里有三个参数,第一个 ...

  7. phpcms v9二次开发之模型类的应用(1)

    在<phpcms二次开发之模型类model.class.php>中讲到了模型类的建立方法,接下来我讲一下模型类的应用.      前段时间我基于phpcms v9开发了一个足球网.足球网是 ...

  8. Xamarin开发笔记—设备类&第三方弹窗的使用和注意事项

    一.设备类是Xamarin重要开发组成部分,下面介绍一下设备类的主要用法: //唤醒打电话 Device.OpenUri(new Uri("tel:180xxxxxxxx")); ...

  9. 用Java开发一个工具类,提供似于js中eval函数功能的eval方法

    今天在看到<Java疯狂讲义>中一个章节习题: 开发一个工具类,该工具类提供一个eval()方法,实现JavaScript中eval()函数的功能--可以动态运行一行或多行程序代码.例如: ...

随机推荐

  1. 大型网站的灵魂——性能

    前言     在前一篇随笔<大型网站系统架构的演化>中,介绍了大型网站的演化过程,期间穿插了一些技术和手段,我们可以从中看出一个大型网站的轮廓,但想要掌握设计开发维护大型网站的技术,需要我 ...

  2. ABP(现代ASP.NET样板开发框架)系列之15、ABP应用层——应用服务(Application services)

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之15.ABP应用层——应用服务(Application services) ABP是“ASP.NET Boiler ...

  3. 菜鸟学Struts2——HelloWorld

    写在前面 自从工作后就过上了只有一个月记忆的生活,太健忘,很多学过的东西因为用得少便忘记了,第二次学习struts,为了以后便于查阅,开始自己的博客之旅.Struts的学习还是从Hello World ...

  4. ASP.NET AntiXSS的作用

    XSS跨站脚本攻击        是指用户输入HTML编码对网站进行跨站攻击.            通过使用FCKeditor.FreeTextBox.Rich TextBox.Cute Edito ...

  5. webpack入门之简单例子跑起来

    webpack介绍 Webpack是当下最热门的前端资源模块化管理和打包工具,它可以将很多松散的模块按照依赖和规则打包成符合生产环境部署的前端资源,还可以将按需加载的模块进行代码分割,等到实际需要的时 ...

  6. ★Kali信息收集~3.子域名系列

    ★3.1Netcraft :子域名查询  官网:http://searchdns.netcraft.com/ 输入要查询的域名,即可得知子域名 3.2Fierce :子域名查询 概述: fierce ...

  7. 【解决方案】 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userHandler': Injection of resource dependencies failed;

    一个错误会浪费好多青春绳命 鉴于此,为了不让大家也走弯路,分享解决方案. [错误代码提示] StandardWrapper.Throwableorg.springframework.beans.fac ...

  8. DDD 领域驱动设计-领域模型中的用户设计

    上一篇:<DDD 领域驱动设计-如何控制业务流程?> 开源地址:https://github.com/yuezhongxin/CNBlogs.Apply.Sample(代码已更新,并增加了 ...

  9. HTTP Method详细解读(`GET` `HEAD` `POST` `OPTIONS` `PUT` `DELETE` `TRACE` `CONNECT`)

    前言 HTTP Method的历史: HTTP 0.9 这个版本只有GET方法 HTTP 1.0 这个版本有GET HEAD POST这三个方法 HTTP 1.1 这个版本是当前版本,包含GET HE ...

  10. How do servlets work-Instantiation, sessions, shared variables and multithreading[reproduced]

    When the servletcontainer (like Apache Tomcat) starts up, it will deploy and load all webapplication ...