Fluent Nhibernate and Stored Procedures

简介: DROP TABLE DepartmentGOCREATE TABLE Department( Id INT IDENTITY(1,1) PRIMARY KEY, DepName VARCHAR(50), PhoneNumber VARCHAR(50))GOCREATE PROCEDURE [dbo].[GetDepartmentId] ( @Id INT )AS
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>
        /// 存储过程
        /// </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);
                        }
                    }
                }


目录
相关文章
|
10月前
|
测试技术 Shell Go
译 | Prefer table driven tests(一)
译 | Prefer table driven tests
52 0
|
10月前
|
存储 测试技术 Go
译 | Prefer table driven tests(二)
译 | Prefer table driven tests(二)
61 0
|
10月前
|
缓存 测试技术 Go
译 | Prefer table driven tests(三)
译 | Prefer table driven tests(三)
57 0
sbs
|
SQL 存储 监控
One SQL to Rule Them All: An Efficient and Syntactically Idiomatic Approach to Management of Streams and Tables 论文翻译
One SQL to Rule Them All: An Efficient and Syntactically Idiomatic Approach to Management of Streams and Tables[文件: One SQL to Rule Them All- An Efficient and Syntactically Idiomatic Approach to Manag
sbs
163 0
One SQL to Rule Them All: An Efficient and Syntactically Idiomatic Approach to Management of Streams and Tables 论文翻译
How to create ABAP implicit enhancement implementation
How to create ABAP implicit enhancement implementation
114 0
How to create ABAP implicit enhancement implementation
strange behavior:why u31000 is accessed for Extension project
Created by Wang, Jerry, last modified on May 20, 2015
92 0
strange behavior:why u31000 is accessed for Extension project
|
SQL 存储 算法
《Optimization of Common Table Expressions in MPP Database Systems》论文导读
Optimization of Common Table Expressions in MPP Database Systems
《Optimization of Common Table Expressions in MPP Database Systems》论文导读