這幾日就在研究這個問題
尋遍網上,也沒找到個解決方法只有
在NHibernate裡執行存儲過程 裡面操作NHibernate的一點方法
於是動手寫成了以下這個類
當然,只能保證測試可以通過,可能還有很多問題,要一個一個解決
1namespace ChAlumna.CastleExt
2{
3 using Castle.ActiveRecord;
4 using Castle.ActiveRecord.Framework;
5 using Castle.ActiveRecord.Framework.Config;
6 using System;
7
8 using NHibernate;
9 using NHibernate.Cfg;
10 using NHibernate.Expression;
11 using System.Collections.Generic;
12 using System.Text;
13 using System.Data;
14 public class Execute
15 {
16 /**//// <summary>
17 /// 使用存儲過程填充實體類,鄒健,2008 1 24
18 /// </summary>
19 /// <typeparam name="T">實體類</typeparam>
20 /// <param name="spname">存儲過程名</param>
21 /// <param name="idict">參數字典</param>
22 /// <returns>一個實體類Ilist</returns>
23 public static IList<T> GetList<T>(string spname, IDictionary<String, Object> idict) {
24 Type type = typeof(T);
25 //Chsword.DoDataBase d = new ChsWord.DoDataBase();
26 StringBuilder sp = new StringBuilder();
27 foreach (string key in idict.Keys) {
28 & sp.AppendFormat(":{0},", key);
29 }
30 if (sp.Length != 0)
31 sp.Length--;
32 string map = String.Format(@"<sql-query name=''{0}''>
33<return class=''{1}''/>
34exec {0} {2}
35</sql-query>",
36 spname,
37 type.Name,
38 sp.ToString()
39 );
40
41 Execute.CreateQueryMapping(type, map);//創建一個SQL-Query
42 ISessionFactoryHolder holder = ActiveRecordMediator.GetSessionFactoryHolder();
43
44 ISession session = holder.CreateSession(type);
45 IQuery query = session.GetNamedQuery(spname);
46
47 foreach (string key in idict.Keys) {
48 &nb query = query.SetParameter(key, idict[key]);
49 }
50 return query.List<T>();
51 }
52 /**//// <summary>
53 /// 創建一個Sql-Query
54 /// </summary>
55 /// <param name="type">類型,做為Key</param>
56 /// <param name="XML">Sql-Query結點</param>
57 public static void CreateQueryMapping(Type type, string XML) {
58 ISessionFactoryHolder holder = ActiveRecordMediator.GetSessionFactoryHolder();
59 Configuration config = holder.GetConfiguration(holder.GetRootType(type));
60
61 // XML = ;
62 config.AddXMLString(
63 string.Format("<hibernate-mappXMLns=''{0}'' assembly=''{1}'' namespace=''{2}''>{3}</hibernate-mapping>",
64 Configuration.MappingSchemaXMLNS,
65 type.Assembly.FullName,
66 type.Namespace,//命名空間
67 XML//內容即<Sql-Query />
68 )
69 );
70 //return config.NamedSQLQuerIEs.Count.ToString();
71 }
72}
73}使用方法如下
IDictionary<string,object> p=new Dictionary<string,Object>();//參數
p.Add("userid", 10000);//參一
p.Add("id", 17002);//參二
p.Add("groupid", 51);//參三
PropertyBag.Add("list",
ChAlumna.CastleExt.Execute.GetList<LogToAccount>(//執行
"Note_Select", p
));
PropertyBag.Add是MonoRail裡的一個系統字典,這裡測試很正常
但是,有以下不中足(我想到的)
此類寫之不易,轉載請留下名