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

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

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

      [Hibernate]在VS2010中應用NHibernate 3.2與MySQL

      在VS2010中應用NHibernate 3.2與MySQL

      羅朝輝 (http://kesalin.cnblogs.com/)

      本文遵循“署名-非商業用途-保持一致”創作公用協議
       
      本文講述了在VS2010中使用NHibernate 3.2與MySQL的一個簡單示例。
       
      工具下載:
      1,NHibernate 3.2
      官網下載:NHibernate,下載完之后,將之解壓到某個目錄,比如:C:\Share\Libraries\NHibernate-3.2.0.GA。
       
      2,創建數據庫
      本文使用前文《在VS2010中應用Entity framework與MySQL》中創建的MySql數據庫表 customer,請參考前文步驟3,在這里就不再多啰嗦了。
       
      3,新建C#控制臺程序 NHSample,然后在工程中新增類Customer,其內容如下:
      // Author:羅朝輝
      // http://kesalin.cnblogs.com/

      namespace NHSample
      {
      class Customer
      {
      public virtual int ID { get; set; }

      public virtual string Address { get; set; }

      public virtual string Name { get; set; }

      override public string ToString()
      {
      return string.Format("Customer Name: {0}, Address: {1}", this.Name, this.Address);
      }
      }
      }
      該類就是將與數據庫進行映射的概念模型類。
       
      4,再向工程中新建一個名為Customer.hbm.xml的xml數據文件,其內容如下:
      <?xml version="1.0" encoding="utf-8" ?>
      <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHSample" assembly="NHSample">
      <class name="Customer" table="Customer">
      <id name="ID" column="id">
      <generator class="assigned"/>
      </id>
      <property name="Address" column="Address" type="String" length="255" />
      <property name="Name" column="Name" type="String" length="255" />
      </class>
      </hibernate-mapping>
      該xml定義概念模型與數據庫之間的映射關系。該xml的命名嚴格遵守如下格式:類名 + .hbm + .xml。
       
      5,下面我們來對數據源進行配置,向工程中新建一個名為 app.config 的xml數據文件,其內容如下:
      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
      <configSections>
      <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
      </configSections>
      <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
      <session-factory>
      <property name="connection.provider">
      NHibernate.Connection.DriverConnectionProvider
      </property>
      <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
      <property name="query.substitutions">hqlFunction=SQLFUNC</property>
      <property name="connection.driver_class">
      NHibernate.Driver.MySqlDataDriver
      </property>
      <property name="connection.connection_string">
      Database=EFSample;Data Source=localhost;User Id=XXX;Password=XXX
      </property>
      <property name="show_sql">false</property>
      <mapping assembly="NHSample" />
      </session-factory>
      </hibernate-configuration>
      </configuration>
      在上面的 xml 中,我們配置了MySql數據源,并在程序中不打算輸出sql語句信息(就是關閉log),注意其中的User Id 和 Password分別是你數據庫的用戶名和密碼。
       
      6,測試
      首先導入NHibernate 庫,右擊 References 選擇 Add Reference,選擇 Browser之前解壓的目錄C:\NHibernate-3.2.0.GA\Required_Bins\,導入Iesi.Collections.dll 和NHibernate.dll兩個庫。
       
      下面來編寫測試代碼,代碼大體與前文一致。
      // Author:羅朝輝
      // http://kesalin.cnblogs.com/

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Reflection;
      using System.Collections;
      using System.Diagnostics;

      using NHibernate;
      using NHibernate.Cfg;
      using NHibernate.Dialect;
      using NHibernate.Driver;
      using NHibernate.Mapping.ByCode;

      namespace NHSample
      {
      class Program
      {
      const int MaxRow = 1000;
      static ISessionFactory sessionFactory = null;

      public static void InitSessionFactory()
      {
      if (sessionFactory == null)
      {
      Configuration config = new Configuration();
      config.AddClass(typeof(NHSample.Customer));

      sessionFactory = config.BuildSessionFactory();
      if (sessionFactory == null)
      {
      Console.WriteLine(" >> Error: Failed to build session factory!");
      }
      }
      }

      public static ISession OpenSession()
      {
      if (sessionFactory == null)
      {
      InitSessionFactory();
      }

      if (sessionFactory == null)
      {
      return null;
      }

      ISession session = sessionFactory.OpenSession();
      if (session == null)
      {
      Console.WriteLine(" >> Error: Failed to open session!");
      }

      return session;
      }

      public static void DeleteData()
      {
      ISession session = OpenSession();
      if (session != null)
      {
      ICriteria crit = session.CreateCriteria(typeof(Customer));
      IList result = crit.List();
      foreach (Customer item in result)
      {
      session.Delete(item);
      }

      session.Flush();
      session.Close();
      }
      }

      public static void InsertData(Customer[] cs)
      {
      ISession session = OpenSession();
      if (session != null)
      {
      foreach (Customer c in cs)
      {
      session.Save(c);
      }

      session.Flush();
      session.Close();
      }
      }

      public static void QueryData()
      {
      ISession session = OpenSession();
      if (session != null)
      {
      for (int i = 1; i <= MaxRow; i++)
      {
      String address = i.ToString();
      IList results = session.CreateQuery("from Customer as t where t.Address = :value").SetString("value", address).List();
      if (results.Count > 0)
      {
      Customer c = (Customer)(results[0]);
      Console.WriteLine(c);
      }
      }

      session.Close();
      }
      }

      protected static object LoadObjectByProperty(ISession session, string typeName, string propertyName, string propertyValue)
      {
      IList results = session.CreateQuery("from " + typeName + " as t where t." + propertyName + " = :value").SetString("value", propertyValue).List();
      if (results.Count > 0)
      return (results[0]);
      else
      return null;
      }

      protected static Customer LoadCustomerByName(ISession session, string name)
      {
      return (LoadObjectByProperty(session, "Customer", "Name", name) as Customer);
      }

      protected static Customer LoadCustomerByAddress(ISession session, string address)
      {
      return (LoadObjectByProperty(session, "Customer", "Address", address) as Customer);
      }

      static void Main(string[] args)
      {
      Customer[] cs = new Customer[MaxRow];
      for (int i = 1; i <= MaxRow; i++)
      {
      StringBuilder sb = new StringBuilder();
      sb.Append("用戶");
      sb.Append(i);

      Customer c = new Customer();
      c.ID = i;
      c.Name = sb.ToString();
      c.Address = i.ToString();

      cs[i - 1] = c;
      }

      Console.WriteLine("=================== TEST START ===================");

      DeleteData();

      Console.WriteLine(">> Storage test start...");
      Stopwatch sw = Stopwatch.StartNew();

      InsertData(cs);

      sw.Stop();
      Console.WriteLine("<< Store data seconds: " + sw.ElapsedMilliseconds / 1000 + " ( " + sw.ElapsedMilliseconds + " miliseconds)");

      Console.WriteLine(">> Query test start...");
      sw = Stopwatch.StartNew();

      QueryData();

      sw.Stop();
      Console.WriteLine("<< Query data seconds: " + sw.ElapsedMilliseconds / 1000 + " ( " + sw.ElapsedMilliseconds + " miliseconds)");

      Console.WriteLine(">> Delete test start...");
      sw = Stopwatch.StartNew();

      DeleteData();

      sw.Stop();
      Console.WriteLine("<< Delete data seconds: " + sw.ElapsedMilliseconds / 1000 + " ( " + sw.ElapsedMilliseconds + " miliseconds)");

      Console.ReadLine();
      }
      }
      }
       
       
       
       
      posted @ 2012-03-11 16:51  飄飄白云  閱讀(2228)  評論(0)    收藏  舉報
      本博客遵循 Creative Commons License “署名-非商業用途-保持一致”創作共用協議。 與我聯系
      主站蜘蛛池模板: 韩国三级+mp4| √新版天堂资源在线资源 | 国产精品一区二区无线| 国产高清小视频一区二区| 平塘县| 久在线视频播放免费视频| 亚洲人妻精品一区二区| 日本久久99成人网站| 国产美女在线精品免费观看| 樱花草在线社区www| 成全我在线观看免费第二季| 衣服被扒开强摸双乳18禁网站| 国产午夜福利高清在线观看| 国产中文字幕在线精品| 大地资源中文第二页日本| 亚洲aⅴ男人的天堂在线观看| 最新国内精品自在自线视频| 久久夜色精品国产亚洲av| 欧美性受xxxx白人性爽| 国产精品一区二区不卡91| 亚洲精品宾馆在线精品酒店| 高清破外女出血AV毛片| 亚日韩精品一区二区三区| 全球成人中文在线| 人妻有码av中文字幕久久琪| 欧美视频网站www色| 亚洲精品国产av成人网| 日本高清中文字幕免费一区二区| 最新AV中文字幕无码专区| 国产一区二区在线观看粉嫩| 日韩av一区二区三区不卡| 国产一区二区三区色成人| 亚洲av男人电影天堂热app | 国产精品国产三级国快看| 国产农村老熟女国产老熟女| 国产成人精品无码播放| 奇米777四色成人影视| 久久一日本道色综合久久| 国产精品香港三级国产av| 亚洲天堂成人网在线观看| 国产精品午夜福利91|