使用Spring.net中对Ado.net的抽象封装来访问数据库

     Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序。它提供了很多方面的功能,比如依赖注入、面向方面编程(AOP)、数据访问抽象及ASP.NET扩展等等。Spring.NET以Java版的Spring框架为基础,将Spring.Java的核心概念与思想移植到了.NET平台上。

要采用spring.net的数据访问抽象来访问sql server数据库,首先要引用spring.net的相应程序集spring.core,spring.data,common.Logging等,从 AdoDaoSupport 派生一个用于数据访问的类,然后项目的配置文件app.config中要添加相应的配置,然后就可以访问数据库了。

1.从 AdoDaoSupport 派生一个用于数据访问的类AdoDaoBase.cs

namespace AdoNetAccess
{
public class AdoDaoBase:AdoDaoSupport
{
private string cmdText = @"select Address, City, CompanyName, ContactName, " +
"ContactTitle, Country, Fax, CustomerID, Phone, PostalCode, " +
"Region from Customers "; public DataTable GetCustomer()
{
return AdoTemplate.DataTableCreate(CommandType.Text, cmdText);
} }
}

2.app.config要添加如下的配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
</sectionGroup>
<sectionGroup name="spring">
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
<arg key="configType" value="INLINE"/>
</factoryAdapter>
</logging>
</common>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="Logs\%date{yyyMMdd}.log"/>
<appendToFile value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level - %message%newline"/>
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="FileAppender"/>
</root>
<logger name="Spring">
<level value="WARN"/>
</logger>
<logger name="Clubank">
<level value="DEBUG"/>
</logger>
</log4net> <spring>
<parsers>
<parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data"/>
</parsers> <context>
<resource uri="config://spring/objects"/>
</context> <objects xmlns="http://www.springframework.net" xmlns:db="http://www.springframework.net/database" xmlns:tx="http://www.springframework.net/tx">
<db:provider id="dbProvider" provider="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS2005;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=1234;Trusted_Connection=False"/> <object id="adoTemplate" type="Spring.Data.Core.AdoTemplate, Spring.Data">
<property name="DbProvider" ref="dbProvider"/>
</object> <!-- 查询定义 -->
<object id="AdoDaoBase" type="AdoNetAccess.AdoDaoBase, AdoNetAccess">
<!-- 注入 AdoTemplate -->
<property name="AdoTemplate" ref="adoTemplate"/>
</object>
</objects> </spring> </configuration>

3.测试访问数据库,Program.cs中的代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace AdoNetAccess
{
class Program
{
static void Main(string[] args)
{
Spring.Context.IApplicationContext context = Spring.Context.Support.ContextRegistry.GetContext();
Spring.Data.Common.IDbProvider provider = context.GetObject("dbProvider") as Spring.Data.Common.IDbProvider;
Spring.Data.Core.AdoTemplate adoTemplate = context.GetObject("adoTemplate") as Spring.Data.Core.AdoTemplate;
object result = adoTemplate.ExecuteScalar(System.Data.CommandType.Text, "select count(*) from Customers"); AdoDaoBase dao = context.GetObject("AdoDaoBase") as AdoDaoBase;
DataTable dt = dao.GetCustomer(); Console.WriteLine(); }
}
}

上面是一个简单的示例,参考了很多的文章,有:

  1. http://www.cnblogs.com/haogj/archive/2011/06/13/2079928.html
  2. http://www.cnblogs.com/haogj/archive/2011/06/18/2084065.html
  3. http://www.cnblogs.com/GoodHelper/archive/2009/11/20/SpringNet_blogs.html
  4. http://www.cnblogs.com/heartstill/archive/2011/08/25/2153832.html
  5. http://www.cnblogs.com/GoodHelper/archive/2009/11/20/SpringNet_Index.html
  6. http://www.cnblogs.com/MyNameEPC/archive/2009/05/09/1453409.html

谢谢以上博客的博主。

代码下载: SpringNET.rar

最新文章

  1. phpstorm的使用教程
  2. C++数组实现的循环队列
  3. Sql Server Always On主库与附库遇到的问题
  4. 2016.5.27 php测试中敏感度高,怎么调整
  5. 关于只针对ie7浏览器的css问题
  6. js异步加载的三种解决方案
  7. 5.5.3使用terminfo功能标志
  8. python 面向对象、特殊方法与多范式、对象的属性及与其他语言的差异
  9. hdu 3724 Encoded Barcodes
  10. SQL语句对应的LINQ和Lambda语句
  11. hibernate 对象状态异常:object references an unsaved transient instance - save the transient instance before flushing
  12. 低字节序和高字节序相互转换(Little Endian/Big Endian)
  13. 【转】mysql行列转换方法总结
  14. bat执行java程序的脚本解析
  15. hadoop搭建在Ubuntu16.04上
  16. 四种Sandcastle方法生成c#.net帮助类帮助文档
  17. Vim 宏
  18. Foundations of Game Engine Development Volume 1 Mathematics (Eric Lengyel 著)
  19. Real World Parsec --- 一个简便易学的 解释器
  20. 主流的Nosql数据库的对比

热门文章

  1. iOS开发Facebook POP动效库使用教程
  2. win32 应用程序 添加资源
  3. 解决Junit单元测试 找不到类 ----指定Java Build Path
  4. 内部类--毕向东Java基础教程学习笔记
  5. Effective Java 46 Prefer for-each loops to traditional for loops
  6. 匿名PL/SQL
  7. HTTP 协议中的 Content-Encoding 和 Transfer-Encoding(内容编码和传输编码)
  8. Linux Shell 04 数字/字符串/文件测试
  9. java控制台输入
  10. EXECL文件导入数据库