Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅检索单个值_C#_Xml_Nhibernate - Fatal编程技术网

C# 仅检索单个值

C# 仅检索单个值,c#,xml,nhibernate,C#,Xml,Nhibernate,如果您想从数据库中检索一些值,您必须始终使用包含大量数据的类 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_retriev

如果您想从数据库中检索一些值,您必须始终使用包含大量数据的类

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; }
    }
}
在我的例子中,我只想从存储过程中检索一个值,我真的需要使用一个只包含一个值的类吗?
当我不想使用类时,我应该如何在.hbm.xml内部创建,以及如何用c#代码编写

它不是一个IList或类似者。只有一个值是字符串

我仍然无法找到解决这种情况的办法

当您需要一个包含大量数据的类时,可以使用下面的代码

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; }
    }
}
sp_retrieveAllProductList.hbm.xml
执行sp_retrieveAllProductList:Produkt_kategori
public IList RetrieveAllProductWithPriceName和BTN()
{
return _session.GetNamedQuery(“sp_retrieveAllProductList2”)
.SetResultTransformer(Transformers.AliasToBean(产品类型4)))
.List();
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间数据层
{
公共类产品4
{
公共虚拟字符串PK_produkt{get;set;}
公共虚拟字符串Produkt{get;set;}
公共虚拟字符串Produkt_kostnad{get;set;}
公共虚拟字符串btn_namn{get;set;}
}
}

我相信您应该能够使用
.UniqueResult

string value = session.GetNamedQuery("Named_Query").UniqueResult<string>();
string value=session.GetNamedQuery(“命名查询”).UniqueResult();
您应该能够使映射文件非常简单:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="data_layer" namespace="data_layer">
  <sql-query name="sp_single_value">
    exec sp_single_value :my_param
  </sql-query>
</hibernate-mapping>

exec sp_single_值:my_参数

当你说一个值时,你的意思是像字符串或intIn,在这个上下文中,它应该是“string”,那么hbm.xml文件呢?返回标量应该如何编写?@FullMetalGame:Updated——这还没有经过测试,但我认为应该可以