Recent projects have also used the ORM framework--nhibernate that was previously done.
The goal this time to achieve:
1. Simple SQL using the NHibernate session of the Crud method implementation
2. Native SQL implementation for complex SQL
3. The database may have multiple, and may be different database types, such as a system with the ORACLE,B system MSSQL
(1th is simple, you can refer to other blogs; This section focuses on 2nd 3rd, about how to configure the next record for multiple databases)
Complex SQL implementations, and for different databases, it is necessary to load different resource files.
For resource files, please refer to http://blog.csdn.net/config_man/article/details/38038249
Then we need to filter the resource files for different databases.
The idea is as follows:
Oracle database Complex SQL statement file: XXX.Oracle.hbm.xml, XXX. Oracle.hbm.xml
MSSQL database Complex SQL statement file: XXX.SQLServer.hbm.xml, XXX. SQLServer.hbm.xml
MySQL database Complex SQL statement file: XXX.MySQL.hbm.xml, XXX. SQLServer.hbm.xml
Use the configuration file to get the database type, and then genjiu the file name suffix to load what resource file.
The code is as follows:
1 foreach(stringResourceinchmappingassembly.getmanifestresourcenames ())2 {3 //database type for intercepting resource files4 intLastIndexOf = resource. LastIndexOf (". Hbm.xml");5 stringDatabaseType = resource. Substring (0, lastIndexOf);6DatabaseType = databasetype.substring (Databasetype.lastindexof (".")+1);7 8 if(Getdatabasetype (). ToUpper (). Equals ("SQL Server"))9 {Ten //if the resource file is not a SQL Server type, the next loop One if(!databasetype.toupper (). Equals ("SQL Server")) A { - Continue; - } the } - Else if(Getdatabasetype (). ToUpper (). Equals ("ORACLE")) - { - //if the resource file is not of type Oracle, the next loop + if(!databasetype.toupper (). Equals ("ORACLE")) - { + Continue; A } at } - Else if(Getdatabasetype (). ToUpper (). Equals ("MYSQL")) - { - //if the resource file is not a MySQL type, the next loop - if(!databasetype.toupper (). Equals ("MYSQL")) - { in Continue; - } to } + Else - { the //not more than three types of databases, do not load resource files * Continue; $ }Panax Notoginseng using(Stream stream =Mappingassembly.getmanifestresourcestream (Resource)) - { the using(StreamReader reader =NewStreamReader (stream)) + { A _configuration. Addxmlstring (reader. ReadToEnd ()); the } + } -}
which
Mappingassembly is the assembly type,
_configuration is the NHibernate.Cfg.Configuration type
Getdatabasetype () Get the database type
NHibernate Dynamically loading resource files