开发者

Insert using stored procedure from nhibernate

开发者 https://www.devze.com 2023-01-03 00:57 出处:网络
I am using the following code snippets to insert values using stored procedure. the code is executing successfully开发者_如何学编程 but no record is inserted in DB.

I am using the following code snippets to insert values using stored procedure. the code is executing successfully开发者_如何学编程 but no record is inserted in DB.

Please suggest with simple example.

**---- stored procedure--------**
Create PROCEDURE [dbo].[SampleInsert]
    @id int, @name varchar(50)
AS
BEGIN   
    insert into test (id, name) values (@id, @name);
END

**------.hbm file-------**
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <sql-query name="Procedure">
    exec SampleInsert
    :Id,:Name
  </sql-query>
</hibernate-mapping>

**--------c# code to insert value using above sp------**
ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
ISession session = sessionFactory.OpenSession();
IQuery query = session.GetNamedQuery("Procedure");
query.SetParameter("Id", "222");
query.SetParameter("Name", "testsp");
query.SetResultTransformer(new NHibernate.Transform.AliasToBeanConstructorResultTransformer(typeof(Procedure).GetConstructors()[0]));

Regards Jcreddy


First, when are you executing the query?

Second, that SP doesn't return anything, what are you adding that ResultTransformer for?

The code should look like this:

session.GetNamedQuery("Procedure")
       .SetParameter("Id", "222")
       .SetParameter("Name", "testsp")
       .ExecuteUpdate()
0

精彩评论

暂无评论...
验证码 换一张
取 消