Share a code generator recently written, integrating EasyDBUtility database access help class, database document Builder

Source: Internet
Author: User

Share a code generator recently written, integrating EasyDBUtility database access help class, database document Builder

I always wanted to write my own code generator, but I had to put it on hold because of many work tasks. Recently I made up my mind to use my off-duty time to finish writing. Now I want to share it with my friends who need it, the Code Generator integrates the EasyDBUtility database access help class. Currently, only sqlserver databases are supported. The interface is as follows:

Some code is as follows:

Using System; using System. collections. generic; using System. data; using System. data. sqlClient; using System. linq; using System. text; using EasyDBUtility; namespace CodeGenerator {// <summary> // generate sqlserver related code // </summary> public class SqlServer {// obtain all database names public const string Get_DataBaseName_ SQL = "SELECT name FROM Master .. sysDatabases where Name not in ('master', 'model', 'msdb', 'tempdb') ORDER Name "; // get all table names in the specified database public const string Get_Tables_ SQL =" SELECT Name FROM [{0}] .. sysObjects Where XType = 'U' order by Name "; // obtain all views of the specified database in public const string Get_Views_ SQL =" SELECT Name FROM [{0}] .. sysObjects Where XType = 'V' order by Name "; // obtain all the stored procedures in the specified database public const string Get_Procedures_ SQL =" SELECT Name FROM [{0}] .. sysObjects Where XType = 'P' order by Name "; // retrieves information about all columns in a specified table or view. public co Nst string Get_Columns_ SQL = "select. name, B. name typeName,. is_identity,. is_nullable from sys. columns a "+" left join sys. types B on. user_type_id = B. user_type_id where. object_id = object_id ('{0}') order by column_id "; // obtain the primary key information of the specified table. public const string GetPKeys_ SQL =" Declare @ objectid int; set @ objectid = object_id ('{0}'); Select col_name (@ objectid, colid) "+" From sysobjects as o Inner Join sysin Dexes as I On I. name = o. name Inner Join sysindexkeys as k On k. indid = I. indid "+" Where o. xtype = 'pk' and parent_obj = @ objectid and k. id = @ objectid "; // get the data type of the specified column public const string Get_Type_Name_ SQL =" SELECT type_name (user_type_id) as [type_name] FROM sys. columns where [object_id] = object_id ('{0}') and [name] = '{1 }'"; /// <summary> /// generate the business logic layer code /// </summary> /// <param name = "tableName"> table name </pa Ram> /// <param name = "connectionString"> database connection string </param> /// <param name = "paramsData"> Generate parameters </param> /// <returns> </returns> public static string CreateBLL (string tableName, string connectionString, ParamsData paramsData) {string primaryKeyName = string. empty; // primary key name string primaryKeyTypeName = string. empty; // The primary key data type SqlHelper helper = new SqlHelper (connectionString); helper. autoClose = false; // obtain the table Primary Key column name helper. CreateCommand (string. Format (GetPKeys_ SQL, tableName); object primaryKeyObj = helper. ExecuteScalar (); if (primaryKeyObj! = Null) {primaryKeyName = primaryKeyObj. toString (); helper. commandText (string. format (Get_Type_Name_ SQL, tableName, primaryKeyName); primaryKeyTypeName = helper. executeScalar (). toString ();} else {// obtain information about all columns in the table helper. commandText (string. format (SqlServer. get_Columns_ SQL, tableName); List <ColumnsData> columnList = helper. executeReader <ColumnsData> (); ColumnsData columnsData = (from col in columnList Where col. Is_identity = true select col). FirstOrDefault (); if (columnsData! = Null) {primaryKeyName = columnsData. name; primaryKeyTypeName = columnsData. typeName ;}} helper. close (); if (string. isNullOrEmpty (primaryKeyName) | string. isNullOrEmpty (primaryKeyTypeName) {throw new Exception ("the primary key or auto-increment column is not found in table" + tableName + "", and BLL Code cannot be generated! Please set the table's primary key or auto-incrementing column and try again! ");} String deleteThePrefixTheTableName = frmMain. getDeleteThePrefixTheTableName (tableName, paramsData); // The generated class name string parameterName = primaryKeyName. substring (0, 1 ). toLower () + primaryKeyName. substring (1); StringBuilder bll = new StringBuilder (); bll. append ("using System. collections. generic; \ r \ n "); if (paramsData. architecture = Architecture. reflection factory mode) bll. append ("using DALFactory; \ r \ n"); bll. append ("using" + paramsData. IDALNamespace + "\ r \ n"); bll. append ("using" + paramsData. modelNamespace + "\ r \ n"); bll. append ("namespace" + paramsData. BLLNamespace + "\ r \ n"); bll. append ("{\ r \ n"); bll. append ("public class" + deleteThePrefixTheTableName + paramsData. BLLSuffix + "\ r \ n"); bll. append ("{\ r \ n"); if (paramsData. architecture = Architecture. reflection factory mode) {bll. append ("private static readonly I" + deleteThePrefixTheTableName + "dal = DataAccess. createInstance <I "+ deleteThePrefixTheTableName +"> (\ "" + deleteThePrefixTheTableName + paramsData. DALSuffix + "\"); \ r \ n ");} else if (paramsData. architecture = Architecture. simple Layer 3) {bll. append ("private static readonly" + deleteThePrefixTheTableName + paramsData. DALSuffix + "dal = new" + deleteThePrefixTheTableName + paramsData. DALSuffix + "(); \ r \ n");} // Add the method if (paramsData. createInsertMethod) {bll. append ("public static bool" + paramsData. insertMethodName + "(" + deleteThePrefixTheTableName + paramsData. modelSuffix + "data) \ r \ n"); bll. append ("{\ r \ n"); bll. append ("return dal. "+ paramsData. insertMethodName + "(data); \ r \ n"); bll. append ("} \ r \ n");} // Delete if (paramsData. createDeleteMethod) {bll. append ("public static bool" + paramsData. deleteMethodName + "(" + ChangeToCSharpType (primaryKeyTypeName, false) + "" + parameterName + ") \ r \ n"); bll. append ("{\ r \ n"); bll. append ("return dal. "+ paramsData. deleteMethodName + "(" + parameterName + "); \ r \ n"); bll. append ("} \ r \ n");} // Delete if (paramsData. createBatchDeleteMethod) {bll. append ("public static bool" + paramsData. batchDeleteMethodName + "(" + ChangeToCSharpType (primaryKeyTypeName, false) + "[]" + parameterName + ") \ r \ n"); bll. append ("{\ r \ n"); bll. append ("return dal. "+ paramsData. batchDeleteMethodName + "(" + parameterName + "); \ r \ n"); bll. append ("} \ r \ n");} // modify if (paramsData. createUpdateMethod) {bll. append ("public static bool" + paramsData. updateMethodName + "(" + deleteThePrefixTheTableName + paramsData. modelSuffix + "data) \ r \ n"); bll. append ("{\ r \ n"); bll. append ("return dal. "+ paramsData. updateMethodName + "(data); \ r \ n"); bll. append ("} \ r \ n");} // query if (paramsData. createSelectMethod) {bll. append ("public static" + deleteThePrefixTheTableName + paramsData. modelSuffix + "" + paramsData. selectMethodName + "(" + ChangeToCSharpType (primaryKeyTypeName, false) + "" + parameterName + ") \ r \ n"); bll. append ("{\ r \ n"); bll. append ("return dal. "+ paramsData. selectMethodName + "(" + parameterName + "); \ r \ n"); bll. append ("} \ r \ n");} // query all if (paramsData. createSelectAllMethod) {bll. append ("public static IList <" + deleteThePrefixTheTableName + paramsData. modelSuffix + ">" + paramsData. selectAllMethodName + "() \ r \ n"); bll. append ("{\ r \ n"); bll. append ("return dal. "+ paramsData. selectAllMethodName + "(); \ r \ n"); bll. append ("} \ r \ n");} bll. append ("} \ r \ n"); bll. append ("}"); return bll. toString ();}}

Execute the file: http://pan.baidu.com/s/1sjptNsh unzip password: shili

The source code has a lot of comments, considering the scalability. If other databases are added, it is easy to add them.

You need to leave an email address for the source code.


Where is the code database generated by the dynamic soft net code generator connected?

It is best not to use soft motion. I used to feel soft and scientific in my studies, but I found it hard to use soft motion after work.
You can write SQL connection statements in WebConfig.
<Deleetask>
<Add key = "conString" value = "server =.; uid = sa; pwd = sa; database = dxchome;"/>
</AppSettings>

Key: Obtain the key name by yourself.
Server: server. Indicates local
Uid: Generally the sa database user name
Psd: Database Password
Database: database Name

Generate a data access layer for the dynamic software code generator

Is it necessary ??
In fact, it is generated based on a template. Do you want to modify the template by yourself ??
Does the dbhelper class conflict with the db class generated by dynamic software? Several methods are automatically generated, executed, and returned based on different databases!
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.