Home  >  Article  >  Database  >  EntLib5.0 DAAB(Data Access Application Block),数据访问程序块

EntLib5.0 DAAB(Data Access Application Block),数据访问程序块

WBOY
WBOYOriginal
2016-06-07 15:44:051436browse

企业库下载:(还有相关视频及帮助文档) http://entlib.codeplex.com/ Entlib5.0 要求.net framework3.5 sp1,或 .net framework 4.0 App.config: ?xml version="1.0" encoding="utf-8" ?configuration configdivs div name="dataConfiguration" type="Micro

企业库下载:(还有相关视频及帮助文档)

http://entlib.codeplex.com/

Entlib5.0 要求.net framework3.5 sp1,或 .net framework 4.0

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configdivs>
        <div name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirepermission="true"></div>
    </configdivs>
    <dataconfiguration defaultdatabase="DbConnString"></dataconfiguration>
    <connectionstrings>
        <add name="DbConnString" connectionstring="Data Source=myip;Initial Catalog=Test;User Id=sa;Password=MyPwd;" providername="System.Data.SqlClient"></add>
      <add name="DbConnString182" connectionstring="Data Source=myAnotherIP;Initial Catalog=DbName;User Id=User;Password=MyPwd" providername="System.Data.SqlClient"></add>
    </connectionstrings>
</configuration>

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using System.Data;

namespace TestEntLib5_0InFramework3_5
{
    class Program
    {
        static void Main(string[] args)
        {
            Database db = DatabaseFactory.CreateDatabase();         //创建一个默认的数据库对象

            //Database db = DatabaseFactory.CreateDatabase("DbConnString182");      //创建一个命名的数据库对象

            //1.ExecuteNonQuery
            /*
            DbCommand dbCmd = db.GetSqlStringCommand(
                @"INSERT INTO T_EntLib (name,birthday,email) VALUES(N'吴1','2008-12-23','a@qq.com');
                INSERT INTO T_EntLib (name,birthday,email) VALUES(N'吴2','2008-12-23','gga@qq.com');
                INSERT INTO T_EntLib (name,birthday,email) VALUES(N'吴3','2008-12-23','a@qq.com');
                INSERT INTO T_EntLib (name,birthday,email) VALUES(N'吴4','2008-12-23','a@qq.com');
                INSERT INTO T_EntLib (name,birthday,email) VALUES(N'吴5','2008-12-23','a@qq.com');");
            int iAffectedNum = db.ExecuteNonQuery(dbCmd);           //返回影响的条数
            Console.WriteLine(iAffectedNum);
            */

            //2. ExecuteDataSet自动开启关闭Connection 

            //var sql = "SELECT TOP 10 * FROM sys_draw ORDER BY id DESC";
            /*
            var sql = "SELECT * FROM T_EntLib";

            DbCommand cmd = db.GetSqlStringCommand(sql);

            //  No need to open the connection; just make the call.
            //	执行时监视:cmd.Connection.State
            DataSet ds = db.ExecuteDataSet(cmd);
            //在此处发现:cmd.Connection.State	Closed	System.Data.ConnectionState
            //说明自动关闭连接
            Console.WriteLine(ds.Tables[0].Rows.Count);
            int col = ds.Tables[0].Columns.Count;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                for (int i = 0; i (sql);       //使用默认的RowMapper

            IRowMapper  rowMapper = MapBuilder<info>.MapAllProperties()
                               .MapByName(x => x.NAME)
                               .DoNotMap(x => x.Email)
                               .DoNotMap(x=>x.Birthday)
                               .Build();
            var result = db.ExecuteSqlStringAccessor<info>(sql, rowMapper);  //使用自定义的RowMapper

            foreach (var item in result)
            {
                Console.WriteLine("ID={0},Name:{1},Birthday:{2},Email:{3}", item.Id, item.NAME, item.Birthday, item.Email);
            }
            Console.ReadKey();
        }
    }
}
</info></info>

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestEntLib5_0InFramework3_5
{
    public class Info
    {
        public int Id { get; set; }
        public string NAME { get; set; }
        public string Email { get; set; }
        public DateTime Birthday { get; set; }
    }
}

  

CREATE TABLE T_EntLib
(
	ID bigint IDENTITY,
	NAME nvarchar(50),
	Birthday datetime,
	Email varchar(50)
)

 http://files.cnblogs.com/wucg/TestEntLib5_0InFramework3_5.zip

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn