1.本文主要介紹了NH的三種查詢方式
2.界面查看
namespace KimismeDemo
{
public partial class Form2 : Form
{
private ISession session;
private ISessionFactory factory;
private ITransaction trans;
public Form2()
{
InitializeComponent();
}
#region 1.初始化NH - private void Form2_Load(object sender, EventArgs e)
private void Form2_Load(object sender, EventArgs e)
{
Configuration config = new Configuration().AddAssembly("Kimisme");
factory = config.BuildSessionFactory();
session = factory.OpenSession();
dgvList.AutoGenerateColumns = false;
}
#endregion
#region 2.0 執行 sql語句 - private void tsmiExecuteSql_Click(object sender, EventArgs e)
private void tsmiExecuteSql_Click(object sender, EventArgs e)
{
string strSql = "select * from T_Student where sId >1";
ISQLQuery sqlQuery = session.CreateSQLQuery(strSql).AddEntity(typeof(Student));
IList<Student> stuList = sqlQuery.List<Student>();
dgvList.DataSource = stuList.ToList();
}
#endregion
#region 3.0 執行存儲過程 - private void btnExecuteStoreProc_Click(object sender, EventArgs e)
private void btnExecuteStoreProc_Click(object sender, EventArgs e)
{
trans = session.BeginTransaction();
IList<Student> stuList = new List<Student>();
ISessionFactoryImplementor imp = factory as ISessionFactoryImplementor;
IDbConnection conn = imp.ConnectionProvider.GetConnection();
IDbCommand cmd = imp.ConnectionProvider.GetConnection().CreateCommand();
try
{
cmd.CommandText = "Pro_GetStudent";
cmd.CommandType = CommandType.StoredProcedure;
IDbDataParameter parameter = cmd.CreateParameter();
parameter.ParameterName = "StudentId";
parameter.Value = 4;
cmd.Parameters.Add(parameter);
cmd.Connection = conn;
IDataReader read = cmd.ExecuteReader();
while (read.Read())
{
Student stu = new Student();
stu.Id = int.Parse(read.GetValue(0).ToString());
stu.Name = read.GetValue(1).ToString();
stu.Age = int.Parse(read.GetValue(2).ToString());
stuList.Add(stu);
}
trans.Commit();
dgvList.DataSource = stuList.ToList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region 4.0 執行hql語句 -private void tsmiExecuteHql_Click(object sender, EventArgs e)
private void tsmiExecuteHql_Click(object sender, EventArgs e)
{
string strHql = "from Student stu where stu.Id >:stuId";
IList<Student> stuList = session.CreateQuery(strHql).SetInt32("stuId", 7).List<Student>();
dgvList.DataSource = stuList.ToList();
}
#endregion
#region 5.0 執行linq語句-聚合函數 - Max
private void tsmiMax_Click(object sender, EventArgs e)
{
var stuList = session.QueryOver<Student>().Where(s => s.Id > 0).List();
int maxAge = (from s in stuList select s.Age).Max();
MessageBox.Show(maxAge.ToString());
}
#endregion
#region 5.1 執行linq語句-聚合函數 - Min
private void tsmiMin_Click(object sender, EventArgs e)
{
var stuList = session.QueryOver<Student>().Where(s => s.Id > 0).List();
int minAge = (from s in stuList select s.Age).Min();
MessageBox.Show(minAge.ToString());
}
#endregion
#region 5.2執行linq語句-聚合函數 - Avg
private void tsmiAvg_Click(object sender, EventArgs e)
{
var stuList = session.QueryOver<Student>().Where(s => s.Id > 0).List();
double avgAge = (from s in stuList select s.Age).Average();
MessageBox.Show(avgAge.ToString());
}
#endregion
#region 5.3執行linq語句-聚合函數 - Sum
private void tsmiSum_Click(object sender, EventArgs e)
{
var stuList = session.QueryOver<Student>().Where(s => s.Id > 0).List();
int sumAge = (from s in stuList select s.Age).Sum();
MessageBox.Show(sumAge.ToString());
}
#endregion
#region 5.4 執行linq語句-聚合函數 - Count
private void tsmiCount_Click(object sender, EventArgs e)
{
var stuList = session.QueryOver<Student>().Where(s => s.Id > 0).List();
int countAge = (from s in stuList select s.Age).Count();
MessageBox.Show(countAge.ToString());
}
#endregion
}
}
5.代碼下載
下載地址