使用Spring.net中对Ado.net的抽象封装来访问数据库
使用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(); }
}
}
上面是一个简单的示例,参考了很多的文章,有:
- http://www.cnblogs.com/haogj/archive/2011/06/13/2079928.html
- http://www.cnblogs.com/haogj/archive/2011/06/18/2084065.html
- http://www.cnblogs.com/GoodHelper/archive/2009/11/20/SpringNet_blogs.html
- http://www.cnblogs.com/heartstill/archive/2011/08/25/2153832.html
- http://www.cnblogs.com/GoodHelper/archive/2009/11/20/SpringNet_Index.html
- http://www.cnblogs.com/MyNameEPC/archive/2009/05/09/1453409.html
谢谢以上博客的博主。
代码下载: SpringNET.rar
使用Spring.net中对Ado.net的抽象封装来访问数据库的更多相关文章
- Spring.NET 中的 ADO.NET 数据访问的示例
Spring.NET 1.3.1 中提供了一个使用 AdoTemplate 的完整示例,包括使用泛型和使用非泛型技术进行数据访问,这个示例位于下载的压缩包中\Spring.NET-1.3.1\Spri ...
- 在Golang中如何正确地使用database/sql包访问数据库
本文记录了我在实际工作中关于数据库操作上一些小经验,也是新手入门golang时我认为一定会碰到问题,没有什么高大上的东西,所以希望能抛砖引玉,也算是对这个问题的一次总结. 其实我也是一个新手,机缘巧合 ...
- Spring Boot中使用时序数据库InfluxDB
除了最常用的关系数据库和缓存之外,之前我们已经介绍了在Spring Boot中如何配置和使用MongoDB.LDAP这些存储的案例.接下来,我们继续介绍另一种特殊的数据库:时序数据库InfluxDB在 ...
- Spring.NET中事务管理【转】
http://www.cnblogs.com/GoodHelper/archive/2009/11/16/springnet_transaction.html 浏览了下写的比较清楚. 在.NET FC ...
- spring boot(三):Spring Boot中Redis的使用
spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...
- Spring Boot中的事务管理
原文 http://blog.didispace.com/springboottransactional/ 什么是事务? 我们在开发企业应用时,对于业务人员的一个操作实际是对数据读写的多步操作的结合 ...
- Spring mvc中@RequestMapping 6个基本用法
Spring mvc中@RequestMapping 6个基本用法 spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: Java代码 @Reques ...
- 如何动态在spring mvc中增加bean
阅读对象 搭框架人员,或者其他感兴趣的开发人员 背景 一般来说在业务代码中,加上 @Component, @Service,@Repository, @Controller等注解就可以实现将bean注 ...
- Spring Boot中的注解
文章来源:http://www.tuicool.com/articles/bQnMra 在Spring Boot中几乎可以完全弃用xml配置文件,本文的主题是分析常用的注解. Spring最开始是为了 ...
随机推荐
- iOS开发网络篇—网络编程基础(一)
一.为什么要学习网络编程 1.简单说明 在移动互联网时代,移动应用的特征有: (1)几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图 (2)只有通过网络跟外界进行数据交互.数据更新 ...
- GDI学习之俄罗斯方块续
当前方块对象 #region 定义砖块int[i,j,y,x] Tricks:i为那块砖,j为状态,y为列,x为行 private int[, , ,] Tricks = {{ { {,,,}, {, ...
- ASP.NET调用Web Service
1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...
- cocos2d-x之利用富文本控件遍历xml
1. #ifndef SuperRichText_hpp #define SuperRichText_hpp #include <stdio.h> #include "cocos ...
- Myeclipse6.0 在线安装svn插件
一.官网地址 1.SVN 官方网站:http://subversion.apache.org/ 二.安装步骤 1. 打开Myeclipse,在菜单栏中选择Help→Software Updates→F ...
- ubuntu16.04服务器配置ssh免密登录
原版资料英文,链接在此 https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2 在客户端操作 ssh-k ...
- Web Storage API : LocalStroage
这是一篇详细介绍详细介绍详细介绍_(:з」∠)_ 背景: 当你访问一个页面,并不是丢到服务器,等待用户访问就可以了的.从输入网址到显示网页的全过程,可以参考这里 简单来说,在输入url按下回车键后,首 ...
- 怎样用Zbrush中的Curves Tubes创建手指
之前我们已经能够初步完成了模型的人体躯干,今天的Zbrush教程将继续使用Curves Tubes创建手指,实现更细致的塑形.文章内容仅以fisker老师讲述为例,您也可以按照自己的想法,跟着老师的步 ...
- 在linux下安装某个硬件驱动到方法
东西很简单,几句话就能说清除. 使用lsipc检查你需要安装到硬件,记住硬件到关键型号,去搜索引擎搜索linux下的驱动文件 对文件进行安装简单的解压后基本上是 ./configure &&a ...
- Android组件系列----Android Service组件深入解析
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...