OneTable operation

配置文件:
<?xml version="1.0" encoding="utf-8" ?>

<activerecord>

  <config>
 <add
  key="hibernate.connection.driver_class"  
  value="NHibernate.Driver.SqlClientDriver" />
 <add
  key="hibernate.dialect"      
  value="NHibernate.Dialect.MsSql2000Dialect" />
 <add
  key="hibernate.connection.provider"   
  value="NHibernate.Connection.DriverConnectionProvider" />
 <add
  key="hibernate.connection.connection_string" 
  value="Data Source=.;Initial Catalog=CastleTest;Integrated Security=SSPI" />
  </config>
 
</activerecord>
实体类:
using System;
using Castle.ActiveRecord;
using NHibernate.Expression;

namespace CastleLearning.OneTable
{


    [ActiveRecord("[User]")]
    public class User : ActiveRecordBase
    {
        private int id;
        private string username;
        private string password;

        [PrimaryKey]
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        [Property]
        public string Username
        {
            get { return username; }
            set { username = value; }
        }

        [Property]
        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        public static int GetUsersCount()
        {
            return CountAll(typeof(User));
        }

        public static User FindByUserName(string userName)
        {
            // Note that we use the property name, _not_ the column name
            return (User)FindOne(typeof(User), Expression.Eq("Username", userName));
        }

        public static User[] FindAll()
        {
            return (User[]) FindAll(typeof(User));
        }

        public static void DeleteByUserName(string userName)
        {
          DeleteAll(typeof(User),"Username = '"+userName+"'");
        }

    }

}
客户代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using CastleLearning.OneTable;

namespace CastleLearning
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                User user = new User();
                user.Password = "12121";
                user.Username = "nan";
                user.Create();
            }
        }

        private void deleteByUserName_Click(object sender, EventArgs e)
        {
            User.DeleteByUserName(txtUserName.Text.Trim());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label1.Text = User.GetUsersCount().ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            User[] users = User.FindAll();

            if (users == null)
            {
                return;
            }

            foreach (User user in users)
            {
                user.Username = DateTime.Now.ToString();
                user.Update();
            }
        }
    }
}

posted @ 2007-04-02 12:20  南守拥  阅读(309)  评论(0编辑  收藏  举报