ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Nhibernate Test

Nhibernate Test

原创 Linux操作系统 作者:ljm0211 时间:2012-07-02 17:20:38 0 删除 编辑
0. 安装
NHibernate
http://sourceforge.net/project/showfiles.php?group_id=73818
log4net
http://sourceforge.net/project/showfiles.php?group_id=31983&release_id=171808
1. 生成数据库
use master
go
create database NHibernate
go
use NHibernate
go
CREATE TABLE users (
LogonID nvarchar(20) NOT NULL default '0',
Name nvarchar(40) default NULL,
Password nvarchar(20) default NULL,
EmailAddress nvarchar(40) default NULL,
LastLogon datetime default NULL,
PRIMARY KEY (LogonID)
)
go
2. 生成persistent类: User.cs
using System;
//note, in the Quick Guide, it says NHibernate.Demo.QuickStart
//you could use it, then you need to change the class name in
//the User.hbm.xml file
namespace NHibernate.Examples.QuickStart
{
public class User
{
private string id;
private string userName;
private string password;
private string emailAddress;
private DateTime lastLogon;
public User()
{
}
public string Id
{
   get { return id; }
   set { id = value; }
}
public string UserName
{
   get { return userName; }
   set { userName = value; }
}
public string Password
{
   get { return password; }
   set { password = value; }
}
public string EmailAddress
{
   get { return emailAddress; }
   set { emailAddress = value; }
}
public DateTime LastLogon
{
   get { return lastLogon; }
   set { lastLogon = value; }
}

}
}
3. 生成影射文件 User.hbm.xml




  







4. 生成配置文件 TestUser.exe.config


   
        

        




   key="hibernate.connection.provider"         
   value="/NHibernate.Connection.DriverConnectionProvider"
/>
   key="hibernate.dialect"                     
   value="/NHibernate.Dialect.MsSql2000Dialect"
/>
   key="hibernate.connection.driver_class"         
   value="/NHibernate.Driver.SqlClientDriver"
/>
   key="hibernate.connection.connection_string"
   value="Server=localhost;initial catalog=nhibernate;Integrated Security=SSPI"
/>



  
   
  

  
   
   
  






5. 生成测试程序:TestUser.cs
using System;
//using System.Configurations;
using NHibernate;
using NHibernate.Cfg;
using System.Collections;
using NHibernate.Examples.QuickStart;
using NHibernate.Expression;
class TestConfig
{
public static void Main()
{
Configuration cfg = new Configuration();
cfg.AddAssembly("NHibernate.Examples");
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();

User newUser = new User();
newUser.Id = "joe_cool";
newUser.UserName = "Joseph Cool";
newUser.Password = "abc123";
newUser.EmailAddress = "";
newUser.LastLogon = DateTime.Now;
  
// Tell NHibernate that this object should be saved
session.Save(newUser);
// commit all of the changes to the DB and close the ISession
transaction.Commit();
session.Close();

Console.WriteLine("go check in the database, enter return to continue....");
Console.ReadLine();
//System.Threading.Thread.Sleep(2000);
//Console.WriteLine("enter return to continue....");
//Console.ReadLine();
// open another session to retrieve the just inserted user
session = factory.OpenSession();
User joeCool = (User)session.Load(typeof(User), "joe_cool");
// set Joe Cool's Last Login property
joeCool.LastLogon = DateTime.Now;
session.Update(joeCool);
// flush the changes from the Session to the Database
session.Flush();
//Console.WriteLine("enter return to continue....");
//Console.ReadLine();
IList userList = session.CreateCriteria(typeof(User)).List();
foreach(User user in userList)
{
Console.WriteLine(user.Id + " last logged in at " + user.LastLogon);
}
IList recentUsers = session.CreateCriteria(typeof(User))
     .Add(Expression.Gt("LastLogon", new DateTime(2004, 03, 14, 20, 0, 0)))
     .List();
foreach(User user in recentUsers)
{
Console.WriteLine(user.Id + " last logged in at " + user.LastLogon);
}

session.Close();
}
}
6. copy or reference
NHibernate.dll
HashCodeProvider.dll (needed at runtime)
log4net.dll (needed at runtime)
7. compile User.cs:
csc /t:library /out:NHibernate.Examples.dll /resource:User.hbm.xml User.cs
8. compile TestUser.cs:
csc /r:NHibernate.dll,NHibernate.Examples.dll TestUser.cs
9. run TestUser.exe
F:\test>TestUser
go check in the database, enter return to continue....
joe_cool last logged in at 12/15/2004 9:42:00 PM
joe_cool last logged in at 12/15/2004 9:42:00 PM

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11411056/viewspace-734398/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-05-14

  • 博文量
    272
  • 访问量
    437243