Nhibbench series Learning (2)-use SQL, hql, and linq, nhibernatehql
1. This article mainly introduces three kinds of NH query methods
2. view the interface
Namespace KimismeDemo {public partial class Form2: Form {private ISession session; private ISessionFactory factory; private ITransaction trans; public Form2 () {InitializeComponent () ;}# region 1. initialize 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 run the SQL statement-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 executes the stored procedure-private void btnExecuteStoreProc_Click (object sender, EventArgs e) private void btnExecuteStoreProc_Click (object sender, EventArgs e. 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 execute the hql statement-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 execute the linq statement-aggregate function-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 execute the linq statement-aggregate function-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 execute the linq statement-aggregate function-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 execute the linq statement-aggregate function-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 execute the linq statement-aggregate function-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. Download Code