本文介绍了仅检索单个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要从数据库检索某些值,则必须始终使用应包含大量数据的类.

就我而言,我只想从存储过程中检索一个值,我真的需要使用只包含单个值的类吗?
当我不想使用类时,应该如何在.hbm.xml内部创建以及如何用C#代码编写?

它不是IList< string>或类似.仅单个值是int字符串.

当需要具有包含大量数据的类时,将使用下面的代码.


sp_retrieveAllProductList.hbm.xml

If you want to retrieve some value from a database you always has to use a class that shall contain a lot of data.

In my case, I want to retrieve one value only from a stored procedure and do I really need to use a class that shall contain a single value only?
How should I create inside of the .hbm.xml and how should I write in c# code when I dont want to use a class?

It is not a IList<string> or similiar. A single value only that is int string.

The code below is used when you need to have class that shall contain a lot of data.


sp_retrieveAllProductList.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="data_layer" namespace="data_layer">
  <sql-query name="sp_retrieveAllProductList">
    <return-scalar column="Produkt" type="string" />
    <return-scalar column="Produkt_kategori" type="string" />
    <return-scalar column="btn_namn" type="string" />
    exec sp_retrieveAllProductList :Produkt_kategori
  </sql-query>
</hibernate-mapping>







public IList<Product4> RetrieveAllProductWithPriceNameAndBtn()
    {
        return _session.GetNamedQuery("sp_retrieveAllProductList2")
                        .SetResultTransformer(Transformers.AliasToBean(typeof(Product4)))
                        .List<Product4>();
    }







using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace data_layer
{
    public class Product4
    {
        public virtual string PK_produkt { get; set; }
        public virtual string Produkt { get; set; }
        public virtual string Produkt_kostnad { get; set; }
        public virtual string btn_namn { get; set; }
    }
}

推荐答案


IList<string> names = Session.CreateSQLQuery("EXEC sp_retrieveAllProductList2")
			.List<string>();



如果要返回单个值



If you want to return a single value

Session
    .CreateSQLQuery(sql)
    .UniqueResult<string>();


这篇关于仅检索单个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 05:37