一个实体类:User
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SmartSurvey.Domain
{
public class User
{
public virtual string UserId { get; set; }
public virtual string UserName { get; set; }
public virtual string Password { get; set; }
public virtual string PasswordSalt { get; set; }
public User() { }
public User(string userid,string username,string password)
{
this.UserId = userid;
this.UserName = username;
this.Password = password;
}
}
}
一个印象文件:User.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="SmartSurvey" namespace="SmartSurvey.Domain">
<class name="User" table="Users">
<id name="UserId" type="String" column="UserId" length="15" unsaved-value="0">
<generator class="native"></generator>
</id>
<property name="UserName" not-null="true"/>
<property name="Password"/>
<property name="PasswordSalt"/>
</class>
</hibernate-mapping>
一个接口类:IUserRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SmartSurvey.Domain;
namespace SmartSurvey.Abstract
{
public interface IUserRepository:IUserReposertory<User>
{
User GetUser(string userid);
User Logon(string userid, string password);
User CreateUser(string userid, string username, string password);
bool DeleteUser(string userid);
void UdataUser(User user);
}
}
一个实现增加的方法类:UserRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartSurvey.Domain;
using SmartSurvey.Abstract;
using NHibernate;
using NHibernate.Cfg;
namespace SmartSurvey.Repositories
{
public class UserRepository : IUserRepository
{
#region IUserRepository 成员
public User GetUser(string userid)
{
using (ISession session = NHibernateHelper.OpenSession())
{
return session.Get<User>(userid);
}
}
/// <summary>
/// 验证用户名及密码
/// </summary>
/// <param name="userid"></param>
/// <param name="password"></param>
/// <returns></returns>
public User Logon(string userid, string password)
{
User user = GetUser(userid);
if (user == null)
throw new Exception(string.Format("用户名[{0}]不存在。",userid));
if (!PasswordHelper.CheckPassword(password, user.Password, user.PasswordSalt))
throw new Exception("密码不匹配");
return user;
}
/// <summary>
/// 添加一个用户名
/// </summary>
/// <param name="userid"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
public User CreateUser(string userid, string username, string password)
{
User user = GetUser(userid);
if (user != null)
throw new Exception(string.Format("用户名[{0}]已经存在。", userid));
string passworddsalt, pass;
pass = PasswordHelper.EncodePassword(password, out passworddsalt);
user = new User(userid, username, password);
using (ISession session = NHibernateHelper.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(user);
transaction.Commit();
}
session.Close();
}
return GetUser(userid);
}
一个配置文件:app.config
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<nhibernate>
<add kty="nhibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add kty="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<add kty="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="hibernate.connection.connection_string"
value="Data Source=.;Initial Catalog=SmartSurvey;User Id=sa;Password=1250;"/>
</nhibernate>
</configuration>
问题来了.. 我要怎么调用他们来实现增加数据.
textBox1 textBox2 textBox3 三个文本框
在 private void button1_Click(object sender, EventArgs e)
{
//我应该怎么写代码. 我试了好多次就是显示有错误 一天了都没有解决 我是新手 希望各位大哥帮帮我 把实现代码告诉我 如果实现了我给100分
}
实体化一下就行啊
NHibernate nh=new NHibernate();
nh.UserRepository();
应该就可以实现
var sessionfactory=(new Configuration()).Configure().BuildSessionFactory();
using (ISession session = sessionfactory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(new User("xxx","xxxx"));
transaction.Commit();
}
session.Close();
}
注意hibernate.cfg.xml文件要在输出目录中