<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Enterprise Library 2.0 Hands On Lab 翻譯(2):數據訪問程序塊(二)

      練習2:存儲過程和使用程序塊更新數據

      該練習將示范如何用數據訪問應用程序調用存儲過程,并使用強類型的DataSet來更新數據。

       

      第一步

      打開DataEx2.sln項目,默認的安裝路徑應該為C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Data Access\exercises\ex02\begin,并編譯。

       

      第二步 在QuickStarts數據庫中添加Categories數據表

      運行批處理文件SetUpEx02.bat,它默認的路徑安裝路徑為C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Data Access\exercises\ex02\DbSetup,默認的服務器實例為(local)\SQLEXPRESS,如果需要修改,用記事本打開SetUpEx02.bat,修改為自己的德數據庫服務器實例。執行后將會在數據庫中創建Categories數據表和存儲過程GetCategories,并會在表中插入一些數據。

       

      第三步 回顧應用程序

      在解決方案管理器中,選中MainForm.cs文件,選擇 View | Designer 菜單,應用程序主要是選擇一個特定的Category,它將會加載該類別下的所有產品,允許我們作一些修改并保存。

       

      第四步 實現數據的讀取

      1.在解決方案管理器中選擇MainForm.cs,選擇View | Code 菜單命令,在代碼中添加如下命名空間,在這之前請先添加對DataCommon兩個程序集的引用,可以參考練習一。

      using Microsoft.Practices.EnterpriseLibrary.Data;

      2.在窗體中加入如下私有域,后面將會在多個地方用到該數據庫實例。

      private Database _db = DatabaseFactory.CreateDatabase("QuickStarts Instance");

      3.在MainForm_Load方法中加入如下代碼

      private void MainForm_Load(object sender, System.EventArgs e)

      {
          
      this.cmbCategory.Items.Clear();

          
      // TODO: Use a DataReader to retrieve Categories

          
      using (IDataReader dataReader = _db.ExecuteReader("GetCategories"))

          
      {
              
      // Processing code 

              
      while (dataReader.Read())

              
      {
                  Category item 
      = new Category(

                      dataReader.GetInt32(
      0),

                      dataReader.GetString(
      1),

                      dataReader.GetString(
      2));

                  
      this.cmbCategory.Items.Add(item);

              }


          }


          
      if (this.cmbCategory.Items.Count > 0)

              
      this.cmbCategory.SelectedIndex = 0;

      }

      重載的方法Database.ExecuteReader,有一個字符串類型的參數,通過它來指定存儲過程的名稱,在這里我們不用做任何數據庫連接方面的管理,但是在DataReader使用完畢后釋放很重要,這些都會由上面的代碼來完成,當DataReader釋放后,數據庫連接也將被關閉。

      4.在cmbCategory_SelectedIndexChanged方法中加入如下代碼,它將根據我們選擇的類別來讀取對應的Product的集合。

      private void cmbCategory_SelectedIndexChanged(object sender, System.EventArgs e)

      {
          
      this.dsProducts.Clear();

          Category selectedCategory 
      = (Category)this.cmbCategory.SelectedItem;

          
      if (selectedCategory == null)

              
      return;


          
      // TODO: Retrieve Products by Category

          _db.LoadDataSet(

              
      "GetProductsByCategory",

              
      this.dsProducts,

              
      new string[] "Products" },

              selectedCategory.CategoryId);

      }

      在數據訪問應用程序塊中Database類提供了兩個關于DataSet的方法ExecuteDataSetLoadDataSetExecuteDataSet返回一個新的DataSetLoadDataSet則返回一個已經存在的DataSet

       

      第五步 實現數據的更新

      btnSave_Click方法中加入如下代碼,將會把所有的任何改動更新到數據庫中去。

      private void btnSave_Click(object sender, System.EventArgs e)

      {
          
      // TODO: Use the DataSet to update the Database 

          System.Data.Common.DbCommand insertCommand 
      = null;

          insertCommand 
      = _db.GetStoredProcCommand("HOLAddProduct");

          _db.AddInParameter(insertCommand, 
      "ProductName",

              DbType.String, 
      "ProductName", DataRowVersion.Current);

          _db.AddInParameter(insertCommand, 
      "CategoryID",

              DbType.Int32, 
      "CategoryID", DataRowVersion.Current);

          _db.AddInParameter(insertCommand, 
      "UnitPrice",

              DbType.Currency, 
      "UnitPrice", DataRowVersion.Current);

          System.Data.Common.DbCommand deleteCommand 
      = null;

          deleteCommand 
      = _db.GetStoredProcCommand("HOLDeleteProduct");

          _db.AddInParameter(deleteCommand, 
      "ProductID",

              DbType.Int32, 
      "ProductID", DataRowVersion.Current);

          _db.AddInParameter(deleteCommand, 
      "LastUpdate",

              DbType.DateTime, 
      "LastUpdate", DataRowVersion.Original);

          System.Data.Common.DbCommand updateCommand 
      = null;

          updateCommand 
      = _db.GetStoredProcCommand("HOLUpdateProduct");

          _db.AddInParameter(updateCommand, 
      "ProductID",

              DbType.Int32, 
      "ProductID", DataRowVersion.Current);

          _db.AddInParameter(updateCommand, 
      "ProductName",

              DbType.String, 
      "ProductName", DataRowVersion.Current);

          _db.AddInParameter(updateCommand, 
      "CategoryID",

              DbType.Int32, 
      "CategoryID", DataRowVersion.Current);

          _db.AddInParameter(updateCommand, 
      "UnitPrice",

              DbType.Currency, 
      "UnitPrice", DataRowVersion.Current);

          _db.AddInParameter(updateCommand, 
      "LastUpdate",

              DbType.DateTime, 
      "LastUpdate", DataRowVersion.Current);

          
      int rowsAffected = _db.UpdateDataSet(

              
      this.dsProducts,

              
      "Products",

              insertCommand,

              updateCommand,

              deleteCommand,

              UpdateBehavior.Standard);

      }

      在更新一個數據庫時,需要對DataTable的列和存儲過程中的參數之間作一個映射,重載的方法UpdateDataSet,它通過數據訪問程序塊自動執行更新事務,在這里UpdateBehaviour是可以設置的,它有三種類型:TransactionalContinueStandard

       

      第六步 運行應用程序

      選擇Debug | Start Without Debugging菜單命令并運行應用程序,在Category下拉框中選擇一個類別,觀察如何加載和保存數據。

       

      更多Enterprise Library的文章請參考《Enterprise Library系列文章

      posted @ 2006-10-06 12:17  TerryLee  閱讀(7958)  評論(5)    收藏  舉報
      主站蜘蛛池模板: 四虎成人高清永久免费看| 亚洲精品人妻中文字幕| 日本一区二区三区专线| 国产精品久久久国产盗摄| 美女午夜福利视频一区二区| 欧美野外伦姧在线观看| 日韩精品有码中文字幕| 国产片AV国语在线观看手机版| 亚洲爆乳少妇无码激情| 漂亮人妻被修理工侵犯 | 一区二区国产精品精华液| 在线无码午夜福利高潮视频| 亚洲成在人线在线播放无码| 中国熟妇牲交视频| 人妻精品人妻无码一区二区三区 | 亚洲精品成人A在线观看| 欧美肥老太牲交大战| 精品一区二区三区四区色| 中文字幕一区二区三区久久蜜桃| 亚洲Av综合日韩精品久久久| 国产精品成人一区二区不卡| 国产在线精品福利91香蕉| 男女激情一区二区三区| 里番全彩爆乳女教师| 免费吃奶摸下激烈视频| 国产女人18毛片水真多1| 亚洲中文久久久精品无码| 成人性生交大片免费看r老牛网站| 久久亚洲美女精品国产精品| 美女黄网站人色视频免费国产| 伊人天天久大香线蕉av色| 人人澡人人透人人爽| 久久国产精品不只是精品| 日韩高清亚洲日韩精品一区二区| 国产精品自拍午夜福利| 人妻伦理在线一二三区| 三年片在线观看免费观看高清动漫| 亚洲精品一二三四区| 亚洲国产精品综合色在线| 狂野欧美性猛交免费视频| 国产精品美腿一区在线看|