Wednesday, March 3, 2010

NHibernate

Quickstart guide

1. Create db nhibernate
2. Create table users
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

3. Create the test windows forms app, add the user class
public class User{
private string id, userName, password, emailAddress;
private DateTime lastLogon;

public virtual string Id{
get { return id; }
set { id = value; }
}
public virtual string UserName{
get { return userName; }
set { userName = value; }
}
public virtual string Password{
get { return password; }
set { password = value; }
}
public virtual string EmailAddress{
get { return emailAddress; }
set { emailAddress = value; }
}
public virtual DateTime LastLogon{
get { return lastLogon; }
set { lastLogon = value; }
}
}


Add the mapping file user.hbm.xml and set its build action as embedded resource

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="quickstart.User, quickstart" table="users">
<id name="Id" column="LogonId" type="String" length="20">
<generator class="assigned" />
</id>
<property name="UserName" column="Name" type="String" length="40"/>
<property name="Password" type="String" length="20"/>
<property name="EmailAddress" type="String" length="40"/>
<property name="LastLogon" type="DateTime"/>
</class>
</hibernate-mapping>


add an app.config

<?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="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider </property>
<property name="proxyfactory.factory_class"> NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu </property>
<property name="connection.connection_string">Server=(local);database=NHibernate;Integrated Security=SSPI;</property>
<property name="show_sql">false</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.isolation">ReadCommitted</property>
<property name="use_proxy_validator">true</property>
</session-factory>
</hibernate-configuration>
</configuration>


download and extract nhibernate to a folder
set references from ur winapp to nhibernate.dll, LinFu.Dynamic.Proxy.dll, NHibernate.ByteCode.LinFu.dll

add some handler to ur app and paste this code

Configuration cfg = new Configuration();
cfg.AddAssembly("quickstart");
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 = "joe@cool.com";
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();

find the dbid by running
select * from master..sysdatabases
run profiler create new trace filter for the dbid of ur db (users)
step thru the above code
u should get a trace like this

exec sp_reset_connection
go
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;BEGIN TRANSACTION
go
exec sp_executesql N'INSERT INTO users (Name, Password, EmailAddress, LastLogon, LogonId) VALUES (@p0, @p1, @p2, @p3, @p4)', N'@p0 nvarchar(11),@p1 nvarchar(6),@p2 nvarchar(12),@p3 datetime,@p4 nvarchar(8)', @p0 = N'Joseph Cool', @p1 = N'abc123', @p2 = N'joe@cool.com', @p3 = 'Mar 3 2010 8:54:54:000AM', @p4 = N'joe_cool'
go
COMMIT TRANSACTION
go

if u check the db there should be a record for joe


if wan more info http://groups.google.com/group/nhusers
if wana copy/paste between local comp and remote desktop follow these steps
http://cutecomputer.wordpress.com/2007/01/03/howto-copy-paste-between-local-and-remote-desktop/