2010-09-01 154 views
4

我需要通過nhibernate調用存儲過程,但我不知道如何。 我有簡單的存儲過程:通過nhibernate調用存儲過程

CREATE PROCEDURE InsertDoc 
    @Name nvarchar(50), 
    @Author nvarchar(50), 
    @Link nvarchar(50) 
AS 
    INSERT INTO documents(name, date, author, doclink) 
    VALUES(@Name, CURRENT_TIMESTAMP, @Author, @Link) 

我想這在我的代碼:

public class documents 
{ 
    public int id; 
    public string name; 
    public DateTime date; 
    public string author; 
    public string doclink; 

    public void CreateDocuments(String n,String l,String u) 
    { 
     documents exSample = new documents(); 
     exSample.name = n; 
     exSample.date = DateTime.Now; 
     exSample.author = u; 
     exSample.doclink = l; 

     using (ISession session = OpenSession()) 
     using (ITransaction transaction = session.BeginTransaction()) 
     { 
      //Session.CreateSQLQuery("EXEC :sp_name :start_date :end_date").SetString("sp_name", <>;) 
      session.CreateSQLQuery("EXEC InsertDoc @Name = N'" + exSample.name + "',@Author = N'" + exSample.author + "',@Link = N'" + exSample.doclink + "'"); 
      // session.Save(exSample); 
      transaction.Commit(); 
     } 
    } 

    public ISessionFactory factory; 

    public ISession OpenSession() 
    { 
     if (factory == null) 
     { 
      Configuration conf = new Configuration(); 
      conf.AddAssembly(Assembly.GetCallingAssembly()); 
      factory = conf.BuildSessionFactory(); 
     } 
     return factory.OpenSession(); 
    } 
} 

我調用存儲過程

session.CreateSQLQuery("EXEC InsertDoc @Name = N'" + exSample.name + "',@Author = N'" + exSample.author + "',@Link = N'" + exSample.doclink + "'"); 

在我的映射文件我有以下設置:

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" namespace="WebApplication1" assembly="WebApplication1"> 
    <class name="WebApplication1.documents" table="documents" lazy="false"> 
    <id name="id" access="field"> 
     <generator class="native" /> 
    </id> 
    <property name="name" access="field" column="name" type="String"/> 
    <property name="date" access="field" column="date" type="date"/> 
    <property name="author" access="field" column="author" type="String"/> 
    <property name="doclink" access="field" column="doclink" type="String"/> 
    </class> 
</hibernate-mapping> 

幫我解決這個問題,或者將我聯繫到一些有用的東西。

+0

你得到的錯誤是什麼? – 2010-09-01 18:57:13

+0

我覺得你錯過了NHibernate是ORM的一點。你可以實例化一個'documents'並像在你註釋掉的那一行那樣調用'Save()'。 – Michael 2014-06-18 20:19:11

回答

5
+13

不,NHibernate的父親是Hibernate ...... Ayende更像是一個吉祥物。 – dotjoe 2010-09-01 19:18:39

+0

Leniel Macaferi,CreateSQLQuery執行時沒有錯誤,但沒有將條目添加到表中 – gro 2010-09-01 19:19:25

2

來看,您似乎缺少Query.executeUpdate()一個,所以

session.CreateSQLQuery("EXEC InsertDoc @Name = N'" + exSample.name + "',@Author = N'" + exSample.author + "',@Link = N'" + exSample.doclink + "'").executeUpdate(); 

應該工作,但它好得多綁定的變量programaticly

+7

這似乎是對我的SQL注入攻擊的祕訣...... – 2016-03-30 18:31:56

+0

這不應該是upvoted ...構造動態SQL字符串isn'好的答案。 – Jansky 2016-08-02 10:59:45

2

下面是一個使用存儲過程插入,更新實體映射的例子和數據庫中的行刪除:

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="dbo" 
       assembly="MyAssembly" 
       namespace="MyAssembly.MyNamespace"> 

    <class name="MyEntity" table="my_entity" lazy="false"> 
    <id name="MyId" column="my_id" type="Int64"> 
     <generator class="native" /> 
    </id> 
    <property name="Name" type="string" column="name" /> 
    <property name="Comment" type="string" column="comment" /> 

    <sql-insert xml:space="preserve"> 
     DECLARE @my_id bigint 
     EXECUTE dbo.InsertMyEntity @name = ?, @comment = ?, @my_id = @my_id OUT 
     SELECT @my_id 
    </sql-insert> 
    <sql-update xml:space="preserve"> 
     EXECUTE dbo.UpdateMyEntity @name = ?, @comment = ?, @my_id = ? 
    </sql-update> 
    <sql-delete xml:space="preserve"> 
     EXECUTE dbo.DeleteMyEntity @my_id = ? 
    </sql-delete> 
    </class> 
</hibernate-mapping> 

有了這個映射你可以使用ISession.Save,ISession.UpdateISession.Delete方法來管理你的實體,並保持NHibernate的第一級實體緩存與數據庫同步。

乾杯, Gerke。