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的更多相关文章

  1. 全自动迁移数据库的实现 (Fluent NHibernate, Entity Framework Core)

    在开发涉及到数据库的程序时,常会遇到一开始设计的结构不能满足需求需要再添加新字段或新表的情况,这时就需要进行数据库迁移. 实现数据库迁移有很多种办法,从手动管理各个版本的ddl脚本,到实现自己的mig ...

  2. 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 ...

  3. 【翻译】Fluent NHibernate介绍和入门指南

    英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs ...

  4. 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 ...

  5. Fluent Nhibernate之旅(五)--利用AutoMapping进行简单开发

    Fluent Nhibernate(以下简称FN)发展到如今,已经相当成熟了,在Nhibernate的书中也相应的推荐了使用FN来进行映射配置,之前写的FN之旅至今还有很多人会来私信我问题,说来惭愧, ...

  6. [Fluent NHibernate]第一个程序

    目录 写在前面 Fluent Nhibernate简介 基本配置 总结 写在前面 在耗时两月,NHibernate系列出炉这篇文章中,很多园友说了Fluent Nhibernate的东东,也激起我的兴 ...

  7. [Fluent NHibernate]一对多关系处理

    目录 写在前面 系列文章 一对多关系 总结 写在前面 上篇文章简单介绍了,Fluent Nhibernate使用代码的方式生成Nhibernate的配置文件,以及如何生成持久化类的映射文件.通过上篇的 ...

  8. [MySQL] Stored Procedures 【转载】

    Stored routines (procedures and functions) can be particularly useful in certain situations: When mu ...

  9. 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 ...

随机推荐

  1. Servlet3.0学习总结——基于Servlet3.0的文件上传

    Servlet3.0学习总结(三)——基于Servlet3.0的文件上传 在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileu ...

  2. node-webkit 笔记

    NW.js is an app runtime based on Chromium and node.js. You can write native apps in HTML and JavaScr ...

  3. 移植UE4的模型操作到Unity中

    最近在Unity上要写一个东东,功能差不多就是在Unity编辑器上的旋转,移动这些,在手机上也能比较容易操作最好,原来用Axiom3D写过一个类似的,有许多位置并不好用,刚好在研究UE4的源码,在模型 ...

  4. window_x64微信小程序环境搭建

    所需文件地址如下: http://pan.baidu.com/s/1nv0IHhn(ylk7)   1.下载微信开发工具0.7.0_x64 安装完成后,打开程序,进行微信扫码登录 2.下载微信开发工具 ...

  5. 自定义StyleCop规则

    参考:StyleCopSDK.chm与 Byeah的 编写StyleCop自定义规则教程(一)---编写中文备注的简单校验规则 1.建立“类库”类型的C#项目 2.加入 Microsoft.Style ...

  6. GCC 4.8.2 编译安装

      https://my.oschina.net/u/728245/blog/184550 摘要: GCC 4.8.2 在 CentOS 6.5 下编译安装小记,遇到一些问题并解决. 以前从没有升级过 ...

  7. Http 1.1协议

    HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间数据交换的过程. 1.Http1. ...

  8. Fast 迅捷网络 无线路由器FW323的功能设置

    一.问题的提出 1.有一个无线路由器,型号:Fast 迅捷网络 无线路由器FW323 2.有三个网络层级,第一级,用一个路由器A负责对接互联网,内网IP段为192.168.1.*,网关设置192.16 ...

  9. QQ微信的备份

    一.问题的提出 windows phone上的微信,累积了太多的微信消息,突然提示“数据库占用空间过大,请及时清理” 二.问题的分析 在朋友发起的群聊中,大量的图片.视频,打开后是下载到本机上的,下载 ...

  10. PIN码计算锦集

    1. 腾达,C8:3A:35开头的MAC有效~network路由,MAC有效~以及00B00C开头的MAC有效之外的请您自己发现算法..这里只公布三个MAC地址算法,其余也可以算~这里就不公布出来了. ...