Fluent Nhibernate and Stored Procedures
sql:存储过程
DROP TABLE Department
GO
CREATE TABLE Department
(
Id INT IDENTITY(1,1) PRIMARY KEY,
DepName VARCHAR(50),
PhoneNumber VARCHAR(50)
)
GO CREATE PROCEDURE [dbo].[GetDepartmentId]
( @Id INT )
AS
BEGIN
SELECT *
FROM Department WHERE Department.Id= @Id
END
GO EXEC GetDepartmentId 1
GO
/// <summary>
/// 存储过程
/// </summary>
/// <returns></returns>
static ISessionFactory testSession()
{
// var config = MsSqlConfiguration.MsSql2005.ConnectionString(@"Server=LF-WEN\GEOVINDU;initial catalog=NHibernateSimpleDemo;User ID=sa;Password=520;").ShowSql();
// var db = Fluently.Configure()
// .Database(config)
// .Mappings(a =>
// {
// a.FluentMappings.AddFromAssemblyOf<Form1>();
// a.HbmMappings.AddClasses(typeof(Department));
// });
// db.BuildConfiguration();
//return db.BuildSessionFactory(); ISessionFactory isessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005
.ConnectionString(@"Server=GEOVINDU-PC\GEOVIN;initial catalog=NHibernateSimpleDemo;User ID=sa;Password=520;").ShowSql())
.Mappings(m => m
//.FluentMappings.PersistenceModel
//.FluentMappings.AddFromAssembly();
.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())) //用法注意
//.Mappings(m => m
//.FluentMappings.AddFromAssemblyOf<Form1>())
//.Mappings(m => m
//.HbmMappings.AddFromAssemblyOf<Department>())
//.BuildConfiguration()
.BuildSessionFactory();
return isessionFactory;
} /// <summary>
/// 存储过程 涂聚文测试成功。 WIN7
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
try
{
using (var exc = testSession())
{
using (var st = exc.OpenSession())
{
if (!object.Equals(st, null))
{
//1
string sql = @"exec GetDepartmentId @Id=:Id";// @"exec GetDepartmentId :Id";
IQuery query = st.CreateSQLQuery(sql) //CreateSQLQuery(sql) //GetNamedQuery("GetDepartmentId")
//.SetInt32("Id", 1)
.SetParameter("Id", 1)
.SetResultTransformer(
Transformers.AliasToBean(typeof(Department)));
//.List<Department>(); var clients = query.UniqueResult();// query.List<Department>().ToList(); //不能强制转化 //IList<Department> result = query.List<Department>(); //不是泛值中的集合
Department dep=new Department();
dep = (Department)clients; //无法将类型为“System.Object[]”的对象强制转换为类型
//2
//var clients = st.GetNamedQuery("GetDepartmentId")
// .SetParameter("Id", 1)
// .SetResultTransformer(Transformers.AliasToBean(typeof(Department)))
// .List<Department>().ToList();
MessageBox.Show(dep.DepName);
}
}
}
参考:http://stackoverflow.com/questions/6373110/nhibernate-use-stored-procedure-or-mapping
/// <summary>
/// Activation
///
/// Action
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public IEnumerable<Department> GetDeactivationList(int companyId)
{
var sessionFactory = FluentNHibernateHelper.CreateSessionFactory();// BuildSessionFactory();
var executor = new HibernateStoredProcedureExecutor(sessionFactory);
var deactivations = executor.ExecuteStoredProcedure<Department>(
"GetDepartmentId",
new[]
{
new SqlParameter("Id", companyId),
//new SqlParameter("startDate", startDate),
// new SqlParameter("endDate", endDate),
}); return deactivations;
}
/// <summary>
/// 存储过程操作
/// </summary>
public class HibernateStoredProcedureExecutor : IExecuteStoredProcedure
{ /// <summary>
///
/// </summary>
private readonly ISessionFactory _sessionFactory;
/// <summary>
///
/// </summary>
/// <param name="sessionFactory"></param>
public HibernateStoredProcedureExecutor(ISessionFactory sessionFactory)
{
sessionFactory = FluentNHibernateHelper.CreateSessionFactory();
_sessionFactory = sessionFactory;
}
/// <summary>
///
/// </summary>
/// <typeparam name="TOut"></typeparam>
/// <param name="procedureName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public IEnumerable<TOut> ExecuteStoredProcedure<TOut>(string procedureName, IList<SqlParameter> parameters)
{
IEnumerable<TOut> result; using (var session = _sessionFactory.OpenSession())
{
var query = session.GetNamedQuery(procedureName);
AddStoredProcedureParameters(query, parameters);
result = query.List<TOut>();
} return result;
}
/// <summary>
///
/// </summary>
/// <typeparam name="TOut"></typeparam>
/// <param name="procedureName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public TOut ExecuteScalarStoredProcedure<TOut>(string procedureName, IList<SqlParameter> parameters)
{
TOut result; using (var session = _sessionFactory.OpenSession())
{
var query = session.GetNamedQuery(procedureName);
AddStoredProcedureParameters(query, parameters);
result = query.SetResultTransformer(Transformers.AliasToBean(typeof(TOut))).UniqueResult<TOut>();
} return result;
}
/// <summary>
///
/// </summary>
/// <param name="query"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static IQuery AddStoredProcedureParameters(IQuery query, IEnumerable<SqlParameter> parameters)
{
foreach (var parameter in parameters)
{
query.SetParameter(parameter.ParameterName, parameter.Value);
} return query;
}
}
/// <summary>
///
/// </summary>
public interface IExecuteStoredProcedure
{
TOut ExecuteScalarStoredProcedure<TOut>(string procedureName, IList<SqlParameter> sqlParameters);
IEnumerable<TOut> ExecuteStoredProcedure<TOut>(string procedureName, IList<SqlParameter> sqlParameters);
}
Fluent Nhibernate and Stored Procedures的更多相关文章
- 全自动迁移数据库的实现 (Fluent NHibernate, Entity Framework Core)
在开发涉及到数据库的程序时,常会遇到一开始设计的结构不能满足需求需要再添加新字段或新表的情况,这时就需要进行数据库迁移. 实现数据库迁移有很多种办法,从手动管理各个版本的ddl脚本,到实现自己的mig ...
- Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one SQL statement
Is there any way in which I can clean a database in SQl Server 2005 by dropping all the tables and d ...
- 【翻译】Fluent NHibernate介绍和入门指南
英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs ...
- Home / Python MySQL Tutorial / Calling MySQL Stored Procedures in Python Calling MySQL Stored Procedures in Python
f you are not familiar with MySQL stored procedures or want to review it as a refresher, you can fol ...
- Fluent Nhibernate之旅(五)--利用AutoMapping进行简单开发
Fluent Nhibernate(以下简称FN)发展到如今,已经相当成熟了,在Nhibernate的书中也相应的推荐了使用FN来进行映射配置,之前写的FN之旅至今还有很多人会来私信我问题,说来惭愧, ...
- [Fluent NHibernate]第一个程序
目录 写在前面 Fluent Nhibernate简介 基本配置 总结 写在前面 在耗时两月,NHibernate系列出炉这篇文章中,很多园友说了Fluent Nhibernate的东东,也激起我的兴 ...
- [Fluent NHibernate]一对多关系处理
目录 写在前面 系列文章 一对多关系 总结 写在前面 上篇文章简单介绍了,Fluent Nhibernate使用代码的方式生成Nhibernate的配置文件,以及如何生成持久化类的映射文件.通过上篇的 ...
- [MySQL] Stored Procedures 【转载】
Stored routines (procedures and functions) can be particularly useful in certain situations: When mu ...
- Good Practices to Write Stored Procedures in SQL Server
Reference to: http://www.c-sharpcorner.com/UploadFile/skumaar_mca/good-practices-to-write-the-stored ...
随机推荐
- 64位Linux下编译搭建Nginx1.5与PHP5.5(CentOS6.4)
(1)安装Nginx1.5.2更新Nginx和PHP的依赖包yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng \libp ...
- Spark源码系列(三)作业运行过程
作业执行 上一章讲了RDD的转换,但是没讲作业的运行,它和Driver Program的关系是啥,和RDD的关系是啥? 官方给的例子里面,一执行collect方法就能出结果,那我们就从collect开 ...
- 怎样在EPLAN P8里创建自己想要的电气元件符号
1.打开Eplan P8 ,新建一个名为"新项目"的项目,然后选择菜单"工具"----"主数据"-----"符号库"-- ...
- mem_fun 例子
// functional_mem_fun.cpp // compile with: /EHsc #include <vector> #include <functional> ...
- 如何在wp8 中调试cocos2dx c++ 代码
有的时候在win32上运行良好的cocos2dx程序移植到wp8的时候就出了问题,我们想把断点放到c++代码中,需要设置一下VS 2012 右击项目属性 把ui任务 设置为仅限本机 即可.
- iOS 数据持久化(扩展知识:模糊背景效果和密码保护功能)
本篇随笔除了介绍 iOS 数据持久化知识之外,还贯穿了以下内容: (1)自定义 TableView,结合 block 从 ViewController 中分离出 View,轻 ViewControll ...
- PHP 常见语法 集合
1.die()与exit()的真正区别 die 为 exit 的别名, 执行过程 将释放内存,停止代码执行 echo "begin exec <br/>"; show( ...
- Android SDK Manager无法显示可供下载的未安装SDK解决方案
FAQ: 问下的 我的ANDROID SDK MANAGER里原来下载了一些SDK,但是我现在想重新下载新的SDK,咋Packages列表没显示呢?该怎么办? Answer: 据说dl-ssl.goo ...
- MyBatis知多少(7)持久层
持久层是适合使用MyBatis的地方.在面向对象的系统中,持久层主要关注对象(或者更精确地说应该是存储在那些对象中的数据)的存取.在企业应用程序中持久层通常用关系数据库系统来存储数据,虽然某些情况下其 ...
- 轻应用、Web app 、Native app三者区别关系是什么?
[龙友导读]最近百度公司在大会上宣布推出“轻应用”.轻应用到底是什么呢,和我们说的web app.native app到底有什么区别?是新生物的诞生还是概念的炒作?所以,今天特意为大家整理分享一篇这方 ...