Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
<?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>



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



C#
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; }
    }
}
Posted
Updated 28-May-14 20:36pm
v3

See How to: Create and Execute an SQL Statement that Returns a Single Value[^] for information on how to execute an SQL statement that returns a single value.
 
Share this answer
 
Comments
deelll 28-May-14 18:15pm    
Thank you for your help. I want to use it in relation to Nhibernate
Try this if you want to get a list of string

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


If you want to return a single value

C#
Session
    .CreateSQLQuery(sql)
    .UniqueResult<string>();
 
Share this answer
 
v3
Comments
deelll 29-May-14 2:32am    
Thank you for your help. It is not a list. it is a single value only without any IList<string>. int string only
soumyajayaraj 29-May-14 2:59am    
I have updated the answer for returning a single value.
deelll 30-May-14 9:10am    
How about the hbm.xml file? How should the return-scalar be written?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900