zoukankan      html  css  js  c++  java
  • 使用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();
    
            }
        }
    }

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

    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

  • 相关阅读:
    css笔记
    CSS基础
    HTML常用基础标签
    React-Native中列表SectionList学习
    React-Native中列表FlatList配合网络请求fetch学习
    React-Native选项卡(react-navigation)之createBottomTabNavigator学习
    React-Native导航条(react-navigation)之createStackNavigator学习
    iOS中touch事件传递(响应者链)
    ios中UIView和CALayer关系
    iOS中KVO使用理解
  • 原文地址:https://www.cnblogs.com/blsong/p/5260886.html
Copyright © 2011-2022 走看看