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

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

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

      程序,人生,思想,靈魂。

      歡迎大家訪問我的個人網站 萌萌的IT人

      導航

      [原創]Enterprise Library4.1學習筆記(一)數據訪問

       

      Enterprise Library v4.1 是patterns & practices 小組為.NET Framework 3.5 開發一套企業庫,對企業應用開發非常有幫助,也非常實用。

      沒事的時候研究了下,把學習過程貼出來,希望能給想要學習 Enterprise Library 的人一點幫助。

       

      (一)下載/安裝

      下載地址:http://www.microsoft.com/downloads/details.aspx?FamilyId=1643758B-2986-47F7-B529-3E41584B6CE5&displaylang=en

      下載完后,雙擊安裝,安裝過程是從“下一步”一直到“完成”,沒有必要講。

       

      (二)  新建解決方案(LearnEntLib),并在解決方案下添加web應用程序項目(DataAccessApplicationBlock)。

      解決方案和程序項目建好后,就可以添加數據訪問組件的引用了,如下圖所示:

      添加完引用后,我們就開始配置數據庫參數,不須要手工配置,直接打開就可以了,如下圖:

      新建一個連接,如下圖:

      配置該連接數據源。如下圖:

       

      選擇默認數據庫,如下圖:

      設置完了后,保存退出,再打開web.config文件,發現已經配置好了。

       

      打開default.aspx,轉到代碼頁,添加命名空間:

       

       

      現在我們可以開始寫代碼了。

       先定義一個公共的DataBase對象,

       Database db = DatabaseFactory.CreateDatabase();

      CreateDatabase()----默認的數據庫。
      CreateDatabase(string name)----指定名稱的數據庫。

       

      (1)增/刪/改

      //
      DbCommand insertCommand = db.GetSqlStringCommand("insert into Categories(CategoryName,Description) values(@CategoryName,@Description)");
      db.AddInParameter(insertCommand, 
      "@CategoryName", DbType.String, "測試CategoryName");
      db.AddInParameter(insertCommand, 
      "@Description", DbType.String, "測試Description");
      db.ExecuteNonQuery(insertCommand);

      //
      DbCommand deleteCommand = db.GetSqlStringCommand("delete from Categories where CategoryID=@CategoryID");
      db.AddInParameter(deleteCommand, 
      "@CategoryID", DbType.Int32, 16);
      db.ExecuteNonQuery(deleteCommand);

      //
      DbCommand updateCommand = db.GetSqlStringCommand("update Categories set CategoryName=@CategoryName,Description=@Description where CategoryID=@CategoryID");
      db.AddInParameter(updateCommand, 
      "@CategoryName", DbType.String, "測試CategoryName1");
      db.AddInParameter(updateCommand, 
      "@Description", DbType.String, "測試Description1");
      db.AddInParameter(updateCommand, 
      "@CategoryID", DbType.Int32, 17);
      db.ExecuteNonQuery(updateCommand);

       

       

      (2)一條一條數據讀取。ExecuteReader

      DbCommand reader = db.GetSqlStringCommand("select CategoryID,CategoryName from Categories where CategoryID>@CategoryID");
      db.AddInParameter(reader, 
      "@CategoryID", DbType.Int32, 10);
      using (IDataReader datareader = db.ExecuteReader(reader))
      {
          
      while (datareader.Read())
          {
              
      //DropDownList1.Items.Add(new ListItem(datareader["CategoryName"].ToString(), datareader["CategoryID"].ToString()));
              DropDownList1.Items.Add(new ListItem(datareader.GetString(1), datareader.GetInt32(0).ToString()));
          }

          
      if (DropDownList1.Items.Count > 0)
          {
              DropDownList1.SelectedIndex 
      = 0;
          }
      }

       

       

      (3)返回數據集。ExecuteDataSet和LoadDataSet

      這兩個方法都是返回數據集,不同的地方是:前者返回一個新的數據集,后者返把返回的數據填充到已存在的數據集當中。

      //ExecuteDataSet
      DbCommand selectcategories = db.GetSqlStringCommand("select * from Categories where  CategoryID>@CategoryID");
      db.AddInParameter(selectcategories, 
      "@CategoryID", DbType.Int32, 2);
      GridView1.DataSource 
      = db.ExecuteDataSet(selectcategories);
      GridView1.DataBind();

      //LoadDataSet
      DataSet ds = new DataSet();
      DbCommand selectprod 
      = db.GetSqlStringCommand("select * from Products where CategoryID>@CategoryID;select * from Categories");
      db.AddInParameter(selectprod, 
      "@CategoryID", DbType.Int32, 7);
      //db.LoadDataSet(selectprod, ds, new string[] { "SelectProdues", "Categories" });
      db.LoadDataSet(selectprod, ds, "Categories");

      GridView1.DataSource 
      = ds.Tables["Categories"];
      GridView1.DataBind();

       

       

      (4)返回一行數據。ExecuteScalar

      DbCommand scalar = db.GetSqlStringCommand("select count(*) from Categories where  CategoryID>@CategoryID");
      db.AddInParameter(scalar, 
      "@CategoryID", DbType.Int32, 10);
      int count = 0;
      int.TryParse(db.ExecuteScalar(scalar).ToString(), out count);
      Response.Write(count);

       

       

       

      (5)添加事務

      就拿 '添加' 來寫個例子吧,其它(刪/改)也同理。如下:

      DbCommand insertCommand = db.GetSqlStringCommand("insert into Categories(CategoryName,Description) values(@CategoryName,@Description)");
      db.AddInParameter(insertCommand, 
      "@CategoryName", DbType.String, "測試CategoryName");
      db.AddInParameter(insertCommand, 
      "@Description", DbType.String, "測試Description");

      DbCommand insertCommand2 
      = db.GetSqlStringCommand("insert into T_Perent(PERENTNAME,PERENTINFO) values(@PERENTNAME,@PERENTINFO)");
      db.AddInParameter(insertCommand2, 
      "@PERENTNAME", DbType.String, "測試CategoryName");
      db.AddInParameter(insertCommand2, 
      "@PERENTINFO", DbType.String, "測試Description");

      using (DbConnection conn = db.CreateConnection())
      {
          conn.Open();
          DbTransaction tran 
      = conn.BeginTransaction();
          
      try
          {
              db.ExecuteNonQuery(insertCommand, tran);
              db.ExecuteNonQuery(insertCommand2, tran);
              tran.Commit();
          }
          
      catch
          {
              tran.Rollback();
          }
          conn.Close();
      }

       

      注:上面的方法如果要用到存貯過程,只須要把db.GetSqlStringCommand(sql語句) 改為db.GetStoredProcCommand(存貯過程名)就可以了,別的地方不用修改。

       

      (三)加密連接字符串

      這個在Enterprise Liberary4.1中變得很簡單了。

       

       

      按上圖操作,保存就可以了,保存后,再打開web.config,如下:

       

      已經加密了。

       

      posted on 2009-10-22 16:44  喬幫主  閱讀(861)  評論(1)    收藏  舉報

      主站蜘蛛池模板: 欧美人妻一区二区三区| 国产精品自在拍在线播放| 国产 麻豆 日韩 欧美 久久| 午夜av福利一区二区三区| 狠狠亚洲丁香综合久久| 疯狂的欧美乱大交| 国产精品午夜无码AV天美传媒| 最新国产精品拍自在线观看| 东京热人妻丝袜无码AV一二三区观 | 中文字幕无码不卡在线| 日本一区二区不卡精品| 国产精品九九九一区二区| 国产福利片一区二区三区| 国产精品爽黄69天堂A| 久久国产成人高清精品亚洲| 久久人人爽爽人人爽人人片av| 小伙无套内射老熟女精品| 亚洲男人精品青春的天堂| 亚洲综合色区另类av| 久久天天躁狠狠躁夜夜网站 | 欧美高清狂热视频60一70| 92国产精品午夜福利| 国产久免费热视频在线观看| 国产又色又爽又黄刺激视频| 日本一本无道码日韩精品| 亚洲精品国产中文字幕| 在线观看国产一区亚洲bd| 象州县| 18黑白丝水手服自慰喷水网站| 国产对白老熟女正在播放| 江都市| 国产在线观看免费观看| 高潮喷水抽搐无码免费| 久久婷婷国产精品香蕉| 凌海市| 999福利激情视频| 欧洲亚洲国内老熟女超碰| 国产精品一码二码三码| 亚洲欧美日韩久久一区二区| 2019亚洲午夜无码天堂| 国产精品高清国产三级囯产AV|