1 In some cases, you might need to execute SQL statements, stored procedures, and so on, but NHibernate does not provide a way for us to execute SQL statements, but it can be done in some indirect way. 2 3 The following is a method executesql for executing SQL statements in NHibernate. 4 PublicIList ExecuteSQL (stringquery) {5IList result =NewArrayList ();6 7Isessionfactoryimplementor s =(isessionfactoryimplementor) cfg. Buildsessionfactory ();8IDbCommand cmd =S.connectionprovider.driver.createcommand ();9Cmd.commandtext =query;Ten OneIDbConnection conn =s.openconnection (); A Try { -Cmd. Connection =Conn; -IDataReader rs =cmd. ExecuteReader (); the - while(Rs. Read ()) { - intFieldCount =Rs. FieldCount; - Object[] values =Newobject[FieldCount]; + for(inti =0; i < FieldCount; i + + ) -Values[i] =Rs. GetValue (i); + result. ADD (values); A } at } - finally { - s.closeconnection (conn); - } - - returnresult; in } - to The result of the return is consistent with the results of the NHibernate query (return object[]). + - 1. Idriver Interface the * Idriver interface is the drive of data access, for different data providers (SqlClient, OLE DB, etc.) have different drives, and SqlClient corresponds to the Sqlclientdriver, and OLE DB corresponds to the oledbdriver. $ Panax Notoginseng The Idriver interface is used to get the Connection object, command object, and format the command text. - the 2. Get the database Connection object + A to execute SQL, you must obtain the IDbConnection object, which can be obtained through the session factory. Note that the Isessionfactory interface does not provide operations related to connection objects, which are defined by the Isessionfactoryimplementor interface. the Isessionfactoryimplementor inherits from Isessionfactory, and the implementation class Sessionfactoryimpl of the session factory implements the two interfaces. + - the code to get the connection object is as follows: $Isessionfactoryimplementor factory =(isessionfactoryimplementor) cfg. Buildsessionfactory (); $IDbConnection conn =Factory. OpenConnection (); - - The OpenConnection method obtains the IDbConnection object from the connection provider ConnectionProvider, and the connection provider creates idbconnection from the driver object. the - 3. Get IDbCommand ObjectWuyi the Within NHibernate, data operations are done through IDbCommand objects, and command objects prevent injection attacks and handle some special characters. - Wu get the code for the IDbCommand object: -IDbCommand cmd =Factory. ConnectionProvider.Driver.CreateCommand (); About $ someone might ask, "SqlCommand" (if you have a word with SqlClient), why is it so complicated? - Yes, this is true, and NHibernate is doing it internally. But if we do this directly, that code is not very good portability, if you change the way the database connection, then you need to change the code, and the use of the above code does not require the change of any code. Of course, except for SQL statements. -As for the parameters, it can be handled by Idbcommand.createparameter.
Executing SQL statements in NHibernate