NHibernate - HQL - 添加和更改

添加:

 1         /// <summary>

 2         /// 等待乙方做出回应 A

 3         /// </summary>

 4         private void button2_Click_1(object sender, EventArgs e)

 5         {

 6             //使用NHibernate,进行存储

 7             Chinfo c = new Chinfo();

 8 

 9             c.CtinfoId = ID;

10             c.Goodsinfo = "白玉手镯";

11             c.Buyersinfo = "甲方";

12             c.Sellerinfo = "乙方";

13             c.Infocreatetime = DateTime.Now;//字段Infocreatetime是DateTime类型,所以此处不能ToString.

14             c.Currentgoodsstate = "A";

15             c.Goodsflow = "1000000";

16             c.Goodsflownowtime = DateTime.Now.ToString();

17             c.CourierId = "234235245";

18 

19             try

20             {

21                 NHelper.Session().Save(c);

22                 NHelper.Trans().Commit();

23                 MessageBox.Show("保存成功!", "提示");

24             }

25             catch (Exception ex)

26             {

27                 MessageBox.Show(ex.Message, "提示");

28             }

29 

30             this.btn_A.Enabled = false;//禁用A

31             this.btn_B.Enabled = true;//开启B

32             this.btn_C.Enabled = true;//开启C

33         }

 

 

 

 

修改:

 1         /// <summary>

 2         /// 乙方已同意请求 等待公司做出回应 B 1100000

 3         /// </summary>

 4         /// <param name="sender"></param>

 5         /// <param name="e"></param>

 6         private void button3_Click(object sender, EventArgs e)

 7         {

 8             try

 9             {

10                 IQuery query = NHelper.Session().CreateQuery("from Chinfo c where c.CtinfoId = ?");

11 

12                 query.SetParameter(0, ID);

13 

14                 IList<Chinfo> list = query.List<Chinfo>();

15 

16                 list[0].Currentgoodsstate = "B";

17                 list[0].Goodsflow = "1100000";

18                 list[0].Goodsflownowtime = list[0].Goodsflownowtime + "-" +  DateTime.Now.ToString();

19 

20                 NHelper.Session().Update(list[0]);

21 

22                 NHelper.Trans().Commit();

23 

24                 MessageBox.Show("修改成功!", "提示");

25             }

26             catch (Exception ex)

27             {

28                 MessageBox.Show(ex.Message, "提示");

29             }

30 

31             this.btn_B.Enabled = false;//禁用B

32             this.btn_C.Enabled = false;//禁用C

33             this.btn_D.Enabled = true;//开启D

34             this.btn_E.Enabled = true;//开启E

35         }

 

 

 

NHelper.cs代码:

 1 using System;

 2 using System.Collections.Generic;

 3 using System.Linq;

 4 using System.Text;

 5 using System.Threading.Tasks;

 6 using NHibernate;

 7 using NHibernate.Cfg;

 8 

 9 namespace Changing

10 {

11     public static class NHelper

12     {

13         //变量声明

14         static ISession session = null;

15         static ISessionFactory factory = null;

16         static ITransaction trans = null;

17 

18         //读取配置文件

19         static Configuration cfg = new Configuration().Configure(@"Config/NHibernate.cfg.xml");

20 

21         /// <summary>

22         /// 创建Session工厂

23         /// </summary>

24         /// <returns></returns>

25         public static ISessionFactory Factory()

26         {

27             return factory = cfg.BuildSessionFactory();

28         }

29 

30         /// <summary>

31         /// 创建Session

32         /// </summary>

33         /// <returns></returns>

34         public static ISession Session()

35         {

36             return session = Factory().OpenSession();

37         }

38 

39         /// <summary>

40         /// 创建事务

41         /// </summary>

42         /// <returns></returns>

43         public static ITransaction Trans()

44         {

45             trans = session.BeginTransaction();

46 

47             return trans;

48         }

49     }

50 }

 

你可能感兴趣的:(Hibernate)