在NHibernate裏怎麼執行存儲過程呢?下面有一個簡單的例子,使用的MS SQL Server 2000 NorthWind庫裏的CustOrderHist存儲過程.
先建2個實體類:
1.CustOrderHist : Customer Historical Order Summary
/// Customer Historical Order Summary Object Entity Class
/// </summary>
public class CustOrderHist
{
private string productName;
private int total;
/// <summary>
/// Constructor
/// </summary>
/// <param name="productName">Product Name</param>
/// <param name="total">Total Quantity</param>
public CustOrderHist ( string productName,int total)
{
this.productName = productName;
this.total = total;
}
/// <summary>
/// Product Name
/// </summary>
public string ProductName
{
get { return productName; }
}
/// <summary>
/// Total Quantity
/// </summary>
public int Total
{
get { return total; }
}
/// <summary>
/// Return Product Name And Quantity
/// </summary>
/// <returns></returns>
public override string ToString ( )
{
return "Product Name : " + productName + "; Quantity: " + total;
}
}
映射關係:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="GetCustomerOrderHistory">
<return-scalar column="ProductName" type="String" />
<return-scalar column="Total" type="int" />
exec CustOrderHist :CustomerID
</sql-query>
</hibernate-mapping>
2.Customer:Customer表的2個字段:CustomerID And CompanyName 提供選擇CustomerID
/// Customer Object Entity Class
/// </summary>
public class Customer
{
private string customerID;
private string companyName;
/// <summary>
/// Customer ID
/// </summary>
public string CustomerID
{
get { return customerID; }
set { customerID = value; }
}
/// <summary>
/// Company Name
/// </summary>
public string CompanyName
{
get { return companyName; }
set { companyName = value; }
}
}
映射關係:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernate.Study.Entity.Customer,Entity" table="Customers">
<id name="CustomerID">
<column name="CustomerID" sql-type="string" length="5"/>
<generator class="assigned" />
</id>
<property name="CompanyName">
<column name="CompanyName" sql-type="string" length="40"/>
</property>
</class>
</hibernate-mapping>
{
private ISession session = null;
public void Dispose ( )
{
session.Dispose( );
}
public BllExecPro ( )
{
session = NHLibrary.NHFactory.OpenSession( );
}
public IList<Customer> getCustomerIDList ( )
{
return session.CreateQuery( "FROM Customer" )
.SetMaxResults( 10 )
.List<Customer>( );
}
public IList<CustOrderHist> getCustOrderHistList ( string customerID )
{
IQuery query = session.GetNamedQuery( "GetCustomerOrderHistory" )
.SetString( "CustomerID" , customerID )
.SetResultTransformer(
new NHibernate.Transform.AliasToBeanConstructorResultTransformer(
typeof( CustOrderHist ).GetConstructors( )[0] ) );
return query.List<CustOrderHist>( );
}
}
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataTextField="CompanyName"
DataValueField="CustomerID" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
Width="238px">
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
</form>
{
if ( !IsPostBack )
{
bindDropDownList( );
}
}
private void bindDropDownList ( )
{
BllExecPro bllExecPro = new BllExecPro( );
DropDownList1.DataSource = bllExecPro.getCustomerIDList( );
DropDownList1.DataBind( );
bindGridView( bllExecPro,DropDownList1.SelectedValue );
}
private void bindGridView (BllExecPro bllExecPro, string customerID )
{
if ( bllExecPro == null)
bllExecPro = new BllExecPro( );
GridView1.DataSource = bllExecPro.getCustOrderHistList( customerID );
GridView1.DataBind( );
bllExecPro.Dispose( );
}
protected void DropDownList1_SelectedIndexChanged ( object sender , EventArgs e )
{
bindGridView(null, DropDownList1.SelectedValue );
}