ActiveRecord多表查询,返回DataTable
来源:互联网 发布:淘宝店顺子电脑好不好 编辑:程序博客网 时间:2024/05/17 17:46
一.Hql 返回IList,组合成DataTable
string hql =" from Item I left join I.Products P left join P.FailureModes F "
+" where P.ProductName=?";
Castle.ActiveRecord.Queries.SimpleQuery query =
new Castle.ActiveRecord.Queries.SimpleQuery(typeof(Item),typeof(IList), hql, "aa");
IList item = (IList)Castle.ActiveRecord.ActiveRecordBase.ExecuteQuery(query);
//// 具体要查的字段,要动态传入
private DataTable MakeTable(IList fields,IList item)
{
DataTable table = new DataTable("Data");
DataColumn column = null;
foreach (QueryField field in fields)
{
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.Caption = field.DisplayText;
column.ColumnName = field.DisplayText;
table.Columns.Add(column);
}
DataRow row;
foreach (object[] ol in item)
{
row = table.NewRow();
int j = 0;
foreach (object o in ol)
{
row[table.Columns[j]] = o;
j++;
}
table.Rows.Add(row);
}
return table;
}
二. 通过Sql语句返回DataTable
string ssql = " select I.ItemName,P.ProductName,F.FailureName "
+ " from Item I left join Product P on I.ItemId=P.ItemId "
+ " left join FailureMode F on P.ProductId=F.ProductId "
+ " where p.ProductName='aa'";
Castle.ActiveRecord.Framework.ISessionFactoryHolder sessionHolder =
Castle.ActiveRecord.ActiveRecordMediator.GetSessionFactoryHolder();
NHibernate.ISession session = sessionHolder.CreateSession(typeof(Item));
try
{
IDbCommand command = session.Connection.CreateCommand();
command.CommandText = ssql;
IDataReader rdr = command.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(rdr, LoadOption.Upsert);
dataGridView1.DataSource = dt;
}
finally
{
sessionHolder.ReleaseSession(session);
}
三。 public static IList<T> GetDataSource<T>(Project curProject)
{
try
{
Type t = typeof(T);
if (t == null) return null;
ParameterExpression param = Expression.Parameter(t, "tt");
//c.City=="London"
Expression left = Expression.Property(param,t.GetProperty("Project"));
Expression right = Expression.Constant(curProject);
Expression filter = Expression.Equal(left, right);
Expression pred = Expression.Lambda(filter, param);
//Where(c=>c.City=="London")
IQueryable<T> query= from tt in ActiveRecordLinq.AsQueryable<T>()
select tt;
Expression expr = Expression.Call(
typeof(Queryable),
"Where",
new Type[] { t },
Expression.Constant(query),
pred
);
var ls=query.ToList<T>().AsQueryable<T>().Provider.CreateQuery<T>(expr);
return ls.ToList<T>();
}
catch
{
return null;
}
}
- ActiveRecord多表查询,返回DataTable
- ActiveRecord多表查询
- 多条件查询DataTable返回DataTable
- ActiveRecord 执行原生SQL语句及返回DataTable
- LINQ查询返回DataTable类型
- 根据条件查询返回DataTable
- LINQ查询返回DataTable类型
- 用linq对datatable进行分组查询并返回datatable
- 用linq对datatable分组查询并返回datatable
- 从gridview选中拖拽的多行数据并放在一个datatable中执行DataTable中的查询返回新的DataTable
- 执行一个查询返回新的datatable或dataset
- 如何将查询结果生成DataTable并返回赋值
- 执行一个查询返回新的datatable或dataset
- Castle ActiveRecord的排序查询
- Castle ActiveRecord 的分页查询
- DataTable查询
- DataTable查询
- WCF返回表datatable时的解决
- ssh keygen – 登入免密碼
- fatal error LNK1000: Internal error during IncrBuildImage
- 自动SCP脚本
- httplib HTTP协议客户端实现
- Python常用文件操作参考
- ActiveRecord多表查询,返回DataTable
- 预期中的尤文新帅-孔蒂
- Android MapView 申请apiKey
- viewWillAppear, viewDidAppear not being called, not firing
- tcp 三次握手 四次挥手 详细过程
- 如何生成jar文件
- swing table
- MyEclipse连接SQL Server2000
- 呼叫中心的软电话架构