• EntLib5.0 DAAB(Data Access Application Block),数据访问程序块 简单示例


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

    http://entlib.codeplex.com/

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

    App.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        </configSections>
        <dataConfiguration defaultDatabase="DbConnString" />
        <connectionStrings>
            <add name="DbConnString" connectionString="Data Source=myip;Initial Catalog=Test;User Id=sa;Password=MyPwd;"
                providerName="System.Data.SqlClient" />
          <add name="DbConnString182" connectionString="Data Source=myAnotherIP;Initial Catalog=DbName;User Id=User;Password=MyPwd"
                providerName="System.Data.SqlClient" />
        </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 < col; i++)
                    {
                        Console.Write(dr[i].ToString());
                    }
                    Console.WriteLine();
                }
                */
    
                //3:ExecuteReader
                /**
                 * If the Data Access Application Block methods close the connection before returning the DbDataReader,
                 * the DbDataReader becomes useless to the client code. Instead, the DbDataReader methods indicate 
                 * to the underlying ADO.NET call to automatically close the connection when the DbDataReader is disposed. 
                 * 
                 * */
                /*
                var sql = "SELECT * FROM T_EntLib";
                DbCommand cmd = db.GetSqlStringCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    int col = reader.FieldCount;
                    while (reader.Read())
                    {
                        for (int i = 0; i < col; i++)
                        {
                            Console.Write(reader[i].ToString());
                        }
                        Console.WriteLine();
                    }
                } //在此处之后,发现 cmd.Connection.State 自动变为Closed.
                */
    
                //4: Retrieving Data as Objects 
               
    
                var sql = "SELECT * FROM T_EntLib";
    
                //var result = db.ExecuteSqlStringAccessor<Info>(sql);       //使用默认的RowMapper
    
                IRowMapper < Info > 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();
            }
        }
    }
    

      

    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)
    )
    

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

  • 相关阅读:
    sql server 中 bit 字段的 查询方法
    C# 4.0新特性"协变"与"逆变"以及背后的编程思想
    marginleft是做边距,是宽度 left是定位盒子左上角左边位置的一个点
    什么是企业库 怎么使用企业库 企业库的好处 企业库的目标
    SQL各种Join用法(Full,Left,Out,Inner)
    JavaScript计算两个文本框内数据的乘积(四舍五入保留两位小数)
    SQL VIEW(视图)
    Sql ISNULL() 函数
    left join 和 left outer join 有什么区别
    18个不常见的C#关键字,您使用过几个?
  • 原文地址:https://www.cnblogs.com/wucg/p/2473297.html
Copyright © 2020-2023  润新知