首页 新闻 会员 周边 捐助

在C#Windows窗体中怎么调用NHibernate的UserRepository里的方法.. 急 高手帮帮忙啊

0
悬赏园豆:100 [待解决问题]

一个实体类: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分

         }

冬瓜皮的主页 冬瓜皮 | 初学一级 | 园豆:0
提问于:2010-09-09 12:21
< >
分享
所有回答(3)
0

找这方面的高手 :李永京

HUHU慈悲 | 园豆:9973 (大侠五级) | 2010-09-09 12:55
0

实体化一下就行啊

NHibernate nh=new NHibernate();

nh.UserRepository();

应该就可以实现

js灰灰 | 园豆:7 (初学一级) | 2010-09-10 00:24
还是不行啊.
支持(0) 反对(0) 冬瓜皮 | 园豆:0 (初学一级) | 2010-09-10 08:38
0

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文件要在输出目录中

李永京 | 园豆:3114 (老鸟四级) | 2010-09-10 09:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册