using NHibernate.Engine;
using NHibernate;namespace DataAccess
{
public class UserAccess
{
public UserAccess()
{
cfg.AddAssembly("Entitys");
//session = cfg.BuildSessionFactory().OpenSession();
} private NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
private ISession session = null;
private ITransaction tran = null; /// <summary>
/// NHibernate 调用存储过程
/// </summary>
/// <returns></returns>
public ArrayList ExeProc()
{
ArrayList list = new ArrayList();
ISessionFactoryImplementor imp = (ISessionFactoryImplementor)cfg.BuildSessionFactory();
IDbConnection conn = imp.ConnectionProvider.GetConnection();
IDbCommand cmd = imp.ConnectionProvider.GetConnection().CreateCommand(); try
{
cmd.CommandText = "TestProc"; //存储过程名
cmd.CommandType = System.Data.CommandType.StoredProcedure;
IDbDataParameter parameter = cmd.CreateParameter();
parameter.ParameterName = "id"; //需要的参数
parameter.Value = 3; //为参数赋值
cmd.Parameters.Add(parameter);
cmd.Connection = conn; //设置连接
IDataReader read= cmd.ExecuteReader();
while (read.Read())
{
UserTest collection = new UserTest();
collection.age = read.GetValue(0).ToString();
collection.id = read.GetValue(1).ToString();
collection.LastTime = read.GetValue(2).ToString();
collection.TureName= read.GetValue(3).ToString();
list.Add(collection);
}
}
catch (Exception ex)
{
this.m_error = ex.Message;
}
finally
{
imp.CloseConnection(conn);
}
return list;
} }
-----------展现层调用------------
protected void Button8_Click(object sender, EventArgs e)
{
list = UserTools.ExeProc();
this.GridView1.DataSource = list;
this.GridView1.DataBind();
this.Label2.Text = UserTools.Error;
}--------------sql 存储过程创建方法--------------------
--创建存储过程
alter proc TestProc
(
@id int
)
as
(
select * from login where id=@id)--测试存储过程
exec TestProc 3