castle activerecord mysql_C#使用Castle.ActiveRecord操作数据库

1. Visual Studio解决方案目录结构

2a407c3da412f4e86ae3d9ab369f6286.png

项目ActiveRecordTest引用的有Castle.ActiveRecord所有dll和项目Models

项目Models引用的有Castle.ActiveRecord中的Castle.ActiveRecord.dll和NHibernate.dll

项目Models是数据表的模型类库,单独放在一个项目中便于管理

2. 应用程序配置文件App.config<?xml  version="1.0" encoding="utf-8" ?>

3. 建立数据表结构,例如specialty列名数据类型允许Null值说明

idint否标识,主键

typetinyint否类型:1院,2系,3专业

parentidint否父级id

namevarchar(100)否名称

regtimedatetime是创建时间

4. 建立数据表模型,例如Specialty.csusing System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Castle.ActiveRecord;

namespace Models

{

[ActiveRecord("specialty")]

public class Specialty : ActiveRecordBase

{

[PrimaryKey("id")]

public int id { get; set; }

[Property("type")]

public int type { get; set; }

[Property("parentid")]

public int parentid { get; set; }

[Property("name")]

public string name { get; set; }

[Property("regtime")]

public string regtime { get; set; }

}

}

5. 数据库操作方法using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Configuration;

using Castle.ActiveRecord;

using Castle.ActiveRecord.Framework;

using Castle.ActiveRecord.Framework.Config;

using Models;

using NHibernate;

using NHibernate.Criterion;

using NHibernate.ByteCode.Castle;

using System.Reflection;

namespace ActiveRecordTest

{

public partial class Form1 : Form

{

private static IConfigurationSource source;

public Form1()

{

InitializeComponent();

source = (IConfigurationSource)ConfigurationManager.GetSection("activerecord");

}

//添加记录

private void button1_Click(object sender, EventArgs e)

{

try

{

ActiveRecordStarter.Initialize(source, typeof(Specialty));

Specialty special = new Specialty();

special.type = 3;

special.parentid = 2;

special.name = "计算机科学与技术";

special.regtime = DateTime.Now.ToString();

special.Create();

MessageBox.Show("添加成功");

}

catch (Exception ex)

{

MessageBox.Show("添加失败!" + ex.Message);

}

}

//修改记录

private void button2_Click(object sender, EventArgs e)

{

try

{

ActiveRecordStarter.Initialize(source, typeof(Specialty));

Specialty special = new Specialty();

special = Specialty.Find(3); //获取id=3的记录

special.name = "计算机科学与技术111";

special.u.p.d.a.t.e();

MessageBox.Show("修改成功");

}

catch (Exception ex)

{

MessageBox.Show("修改失败!" + ex.Message);

}

}

//删除记录

private void button3_Click(object sender, EventArgs e)

{

try

{

ActiveRecordStarter.Initialize(source, typeof(Specialty));

Specialty special = new Specialty();

special.id = 3; //删除id=3的记录

special.d.e.l.e.t.e();

MessageBox.Show("删除成功");

}

catch (Exception ex)

{

MessageBox.Show("删除失败!" + ex.Message);

}

}

//读取记录

private void button4_Click(object sender, EventArgs e)

{

ActiveRecordStarter.Initialize(source, typeof(Specialty));

DetachedCriteria query = DetachedCriteria.For(typeof(Specialty));

query.Add(Property.ForName("id").Gt(1)); //查询条件id>1

query.AddOrder(Order.Desc("id")); //排序方式id desc

var result = Specialty.FindAll(query);

foreach (Specialty onespecial in result)

{

textBox1.Text += onespecial.id +", "+ onespecial.name +", "+ onespecial.regtime +"\r\n";

}

}

//批量修改

private void button5_Click(object sender, EventArgs e)

{

ActiveRecordStarter.Initialize(source, typeof(Specialty));

ISessionFactoryHolder sessionHolder = ActiveRecordMediator.GetSessionFactoryHolder();

ISession session = sessionHolder.CreateSession(typeof(Specialty));

try

{

System.Data.IDbCommand command = session.Connection.CreateCommand();

command.CommandText = "u.p.d.a.t.e special set name=name+'123' w.h.e.r.e id<4"; //执行sql语句

command.ExecuteNonQuery();

textBox1.Text = "修改成功";

}

catch (Exception ex)

{

throw ex;

}

sessionHolder.ReleaseSession(session);

}

//批量删除

private void button6_Click(object sender, EventArgs e)

{

ActiveRecordStarter.Initialize(source, typeof(Specialty));

ActiveRecordMediator.d.e.l.e.t.eAll(typeof(Specialty), "id>2"); //批量删除id>2的记录

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值