gpt4 book ai didi

sql - 通过nhibernate调用存储过程

转载 作者:行者123 更新时间:2023-12-03 01:59:24 28 4
gpt4 key购买 nike

我需要通过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>

帮助我解决此问题或将我链接到有用的内容。

最佳答案

以下是使用存储过程插入、更新和删除数据库行的实体映射示例:

<?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.SaveISession.UpdateISession.Delete 方法来管理您的实体并保留NHibernate 一级实体缓存与数据库同步。

干杯,格克。

关于sql - 通过nhibernate调用存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3621155/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com