欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

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

程序员文章站 2022-03-05 12:50:41
...

企业库下载:(还有相关视频及帮助文档) 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:


    

  

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.MapAllProperties()
                               .MapByName(x => x.NAME)
                               .DoNotMap(x => x.Email)
                               .DoNotMap(x=>x.Birthday)
                               .Build();
            var result = db.ExecuteSqlStringAccessor(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();
        }
    }
}

  

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