• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
安安的BLOG
安安目前专注电子商务解决方案^_^
博客园    首页    新随笔    联系   管理    订阅  订阅

Enterprise Library 5.0 Hands On Lab(1):数据访问程序块(一)

第一步  创建数据库,根据 \Data Access\setup\DataAccessQuickStarts.sql
第二步  打开 Data Access\exercises\ex01\begin 目录下 项目文件DataEx1.sln

第三步  查看应用程序
          在Visual Studio解决方案管理器中选择MainForm.cs文件,选择 View | Designer 菜单,
          应用程序包含一个DataGrid和几个Menu,我们将使用数据访问应用程序块来统计数据库中的Customer的数量,并加载数据到DataGrid中。

第四步 实现Customer菜单项

         1.选中CustomerManagment项目,选择 Project | Add Reference … 菜单命令,在打开的对话框中选择Browse,并添加如下三个程序集(Microsoft Enterprise Library 5.0\Bin)。
   Microsoft.Practices.EnterpriseLibrary.Common.dll
   Microsoft.Practices.EnterpriseLibrary.Data.dll
必须添加Common程序集引用,因为Data Access Application Block支持度量机制(Interception)但是IInstrumentationEventProvider是在Common程序集中定义。

原来老的数据处理方法:

config文件

    <connectionStrings>
        <add name="QuickStarts Instance" connectionString="Data Source=.;Initial Catalog=EntLibQuickStarts;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>

程序代码

using Microsoft.Practices.EnterpriseLibrary.Data;

 

            Database db = null;
            //4.1
            db = DatabaseFactory.CreateDatabase("QuickStarts Instance");
            int count = (int)db.ExecuteScalar(CommandType.Text, "select count(1) from Customers with(nolock)");
            string message = string.Format("There are {0} customers in the database", count.ToString());
            MessageBox.Show(message);

另一种通过

引用

Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Interception.dll

   ServiceLocation 这个类是用来 Unity 取数据库实例。

config文件:

<configuration>
    <configSections>
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <dataConfiguration defaultDatabase="QuickStarts Instance" />
    <connectionStrings>
        <add name="QuickStarts Instance" connectionString="Data Source=.;Initial Catalog=EntLibQuickStarts;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

程序代码:

            Database db = null;
            //4.1
            db = EnterpriseLibraryContainer.Current.GetInstance<Database>("QuickStarts Instance");
            int count = (int)db.ExecuteScalar(CommandType.Text, "select count(1) from Customers with(nolock)");
            string message = string.Format("There are {0} customers in the database", count.ToString());
            MessageBox.Show(message);

 

posted @ 2011-06-10 11:17  安安  阅读(367)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3