NHibernate教程2

來源:互聯網
上載者:User

三、NHibernate的使用

1. 配置NHibernate

 

(1)NHibernate的設定檔有兩種:在傳統型應用程式(WinForm)中為App.config,在網頁應用程式(WebForm)中為web.config。兩種方法要添加的配置資訊是一樣的,目標都是告訴NHbiernate使用什麼樣的資料庫,使用哪個庫,使用者密碼分別是什麼。

 

如果使用的是WinForm,將如下文本儲存為App.config放到程式啟動目錄就可以。如果是WebForm,則將下面的文本添加到web.config中

 

<?xml version="1.0" encoding="utf-8" ?>
   <configuration>
      <configSections>
         <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </configSections>
      <nhibernate>
         <!--串連資料提供者 -->
         <add key="hibernate.connection.provider"
         value="NHibernate.Connection.DriverConnectionProvider" />
         <!--串連資料方言最常用的是MsSql2000Dialect -->
         <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
         <!--串連資料驅動類-->
         <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
         <!--資料庫連接字串-->
         <add key="hibernate.connection.connection_string" value="workstation id=BILLGATES;packet size=4096;integrated security=SSPI;initial catalog=CAW;persist security info=False" />
      </nhibernate>
   </configuration>

 

 

(2)然後將NHibernate唯一的一個dll(NHibernate.dll)添加到工程的引用中,步驟如下:

 

à à

 

2. 建立一個用於測試的表

 

這裡需要建立3張表,分別用於儲存:學生、課程、選課資訊。可以用Sql語句來建立,也可以用其他方式建立。建立表的Sql語句如下:

 

CREATE TABLE Student (

 

StudentId int identity (1, 1) PRIMARY KEY NOT NULL,

 

StudentName nvarchar(40) default NULL,

 

StudentPassword nvarchar(20) default NULL,

 

EmailAddress nvarchar(40) default NULL,

 

DateCreated datetime default NULL

 

)

 

 

CREATE TABLE Course (

 

CourseId int identity (1, 1) PRIMARY KEY NOT NULL,

 

CourseName nvarchar(40) default NULL,

 

DateCreated datetime default NULL

 

)

 

 

CREATE TABLE StudentCourse (

 

StudentCourseId int identity (1, 1) PRIMARY KEY NOT NULL,

 

StudentId int,

 

CourseId int,

 

DateCreated datetime default NULL

 

)

 

 

 

 

 

產生的三張表的結構如下所示:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.