Code Generator for self-coding Implementation of database ing entities

Source: Internet
Author: User

In the past, CodeSmith or dynamic software was often used to generate relevant database access code (not like to use Persistence frameworks). CodeSmith's support for Chinese characters was poor and the new version 6.5 was downloaded, the description of table fields in the dynamic software code generator cannot be correctly displayed (Li Tianping's failure). There are several attributes such as MySql's smallint, tinyint, it is too troublesome to modify the ing table of the option again. Although mobile software now provides the module generation function, renaming tables in batch generation is not supported, and the function is a little weak. Therefore, you can write a code generator and generate tables according to your own rules, the comments are also provided. The comments are added, which is very helpful for development. The Code is as follows:

 

Private void CreateModel () {string connectionString = "server = 127.0.0.1; port = 3306; User Id = root; Password = root; database = ipbroadcast; Connect Timeout = 60; treat Tiny As Boolean = False "; MySql. data. mySqlClient. mySqlConnection conn = new MySql. data. mySqlClient. mySqlConnection (connectionString); conn. open (); DataTable table = conn. getSchema ("TABLES"); foreach (DataRow row in table. rows) {DataTable sheetTab Le = GetSchemaTable (conn, row); string tName = row ["TABLE_NAME"]. toString (); CreateEntityFile (conn, tName, sheetTable);} conn. close ();} private DataTable GetSchemaTable (MySqlConnection conn, DataRow row) {string sheetName = row ["TABLE_Name"]. toString (); MySqlCommand com = conn. createCommand (); com. commandType = CommandType. text; com. commandText = "Select * From" + sheetName; IDataReader reader = Com. executeReader (CommandBehavior. schemaOnly); DataTable table = reader. getSchemaTable (); com. dispose (); reader. close (); return table;} private void CreateEntityFile (MySqlConnection conn, string tName, DataTable table) {// define the path of the generated file string tableName = tName; string schemaName = ""; string colName = ""; string path = @ "F: \ IPBroadcastModel \ Model1"; if (! Directory. exists (path) Directory. createDirectory (path); MySqlCommand com = conn. createCommand (); com. commandType = CommandType. text; Dictionary <string, string> dict = new Dictionary <string, string> (); bool isGetComment = false; // write the file string clsName = "Ety" + tableName. substring (3, 1 ). toUpper () + tableName. substring (4); string fileName = clsName + ". cs "; string fullName = Path. combine (path, fileNam E); using (FileStream fs = new FileStream (fullName, FileMode. create) {StreamWriter sw = new StreamWriter (fs, Encoding. UTF8); sw. writeLine ("using System;"); sw. writeLine ("using System. text; "); sw. writeLine ("using System. collections. generic; "); sw. writeLine ("using System. data; "); sw. writeLine ("namespace AEBell. DBManager "); sw. writeLine ("{"); sw. writeLine ("\ t [Serializable]"); sw. writeLine ("\ tpubli C class "+ clsName); sw. writeLine ("\ t {"); foreach (DataRow row in table. rows) {// tableName = row ["BaseTableName"]. toString (); colName = row ["ColumnName"]. toString (); schemaName = row ["BaseSchemaName"]. toString (); if (! IsGetComment) {isGetComment = true; GetFielComment (tableName, schemaName, com, dict);} sw. writeLine ("\ t // <summary>"); sw. writeLine ("\ t //" + dict [colName]); sw. writeLine ("\ t // </summary>"); Type info = row ["DataType"] as Type; string declear = info. name; if (declear. toUpper () = "SBYTE") // to adapt to dynamic softness. Declear = "Byte"; bool isNull = (bool) row ["AllowDBNull"]; if (isNull & info. BaseType. Name = "ValueType") declear + = "? "; Sw. writeLine ("\ t \ tpublic" + declear + "" + colName. substring (0, 1 ). toUpper () + colName. substring (1); sw. writeLine ("\ t {"); sw. writeLine ("\ t \ tget;"); sw. writeLine ("\ t \ tset;"); sw. writeLine ("\ t}");} sw. writeLine ("\ t}"); sw. writeLine ("}"); sw. close () ;}} private static void GetFielComment (string tableName, string schemaName, MySqlCommand com, Dictionary <string, string> dict) {string comment = ""; com. commandText = "Select COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT From INFORMATION_SCHEMA.COLUMNS where table_name = '" + tableName + "' AND table_schema = '" + schemaName + "'"; // AND column_name LIKE '"+ colName +"' "; IDataReader reader = com. executeReader (); while (reader. read () {string colName = reader. getString (0); comment = reader. getString (2); dict [colName] = comment;} reader. close ();}

Although the implementation is not very rigorous, The DAL and BLL layers are not implemented, but those who need it can be implemented quickly. Correct the error.

Related Article

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.