MySQL生成C#實體類


調用MySQL存儲過程來為數據表生成對應的C#實體類,可節省開發人員編碼的時間,提高效率。

執行存儲過程需要“數據庫名稱”和“表名稱”兩個參數:

CALL GenCSharpModel('數據庫名稱', '表名稱');

執行結果如下圖:

 

存儲過程代碼如下:

CREATE DEFINER=`root`@`localhost` PROCEDURE `GenCSharpModel`(in pDataBaseName VARCHAR(255),in pTableName VARCHAR(255))
BEGIN
DECLARE vClassName varchar(255);
declare vClassCode mediumtext;
declare colComment varchar(1024);
declare colType varchar(1024);
declare fName varchar(1024);
declare fNameNew varchar(1024);

DECLARE v_finished INTEGER DEFAULT 0;

DEClARE code_cursor CURSOR FOR 
            SELECT ColumnComment,ColumnType,FieldName FROM temp1; 

DECLARE CONTINUE HANDLER FOR 
            NOT FOUND SET v_finished = 1;

            -- 生成實體類名
            SELECT REPLACE(GROUP_CONCAT(CONCAT(UPPER(LEFT(A.Field,1)),SUBSTRING(A.Field,2,(LENGTH(A.Field)-1)))),',','')
            
            FROM (SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(pTableName,'_',help_topic_id+1),'_',-1) AS Field 
                        FROM mysql.help_topic 
            WHERE help_topic_id < LENGTH(pTableName)-LENGTH(REPLACE(pTableName,'_',''))+1) A 
                        
            into vClassName ;

      -- 創建臨時表
    CREATE TEMPORARY TABLE IF NOT EXISTS  temp1 ENGINE=MyISAM  
    as (
                    SELECT  COLUMN_NAME  AS FieldName, COLUMN_COMMENT AS ColumnComment,
                        case  
                            when DATA_TYPE='bigint' and IS_NULLABLE = 'YES'  then 'long?'
                            when DATA_TYPE='bit' and IS_NULLABLE = 'YES'  then 'bool?'
                            when DATA_TYPE='char' and COLUMN_TYPE = 'char(36)' and IS_NULLABLE = 'YES' then 'Guid?'
                            when DATA_TYPE='date' and IS_NULLABLE = 'YES'  then 'DateTime?'
                            when DATA_TYPE='datetime' and IS_NULLABLE = 'YES' then 'DateTime?'
                            when DATA_TYPE='decimal' and IS_NULLABLE = 'YES' then 'decimal?'
                            when DATA_TYPE='float' and IS_NULLABLE = 'YES' then 'float?'
                            when DATA_TYPE='int' and IS_NULLABLE = 'YES' then 'int?'
                            when DATA_TYPE='numeric' and IS_NULLABLE = 'YES' then 'decimal?'
                            when DATA_TYPE='decimal' and IS_NULLABLE = 'YES' then 'decimal?'
                            when DATA_TYPE='double' and IS_NULLABLE = 'YES' then 'double?'
                            when DATA_TYPE='real' and IS_NULLABLE = 'YES' then 'double?'
                            when DATA_TYPE='smallint' and IS_NULLABLE = 'YES' then 'short?'
                            when DATA_TYPE='mediumint' and IS_NULLABLE = 'YES' then 'int?'
                            when DATA_TYPE='time'  and IS_NULLABLE = 'YES' then 'TimeSpan?'
                            when DATA_TYPE='timestamp' and IS_NULLABLE = 'YES' then 'DateTime?'
                            when DATA_TYPE='tinyint' and COLUMN_TYPE = 'tinyint(1)' and IS_NULLABLE = 'YES' then 'bool?'
                            when DATA_TYPE='tinyint' and IS_NULLABLE = 'YES' then 'byte?'
                            when DATA_TYPE='year'  and IS_NULLABLE = 'YES' THEN 'uint?'
                            
                            when DATA_TYPE='bigint' then 'long'
                            when DATA_TYPE='binary' then 'byte[]'
                            when DATA_TYPE='bit' then 'bool'                
                            when DATA_TYPE='char' and COLUMN_TYPE = 'char(36)' then 'Guid'                    
                            when DATA_TYPE='date' then 'DateTime'                
                            when DATA_TYPE='datetime' then 'DateTime'    
                            when DATA_TYPE='decimal' then 'decimal'    
                            when DATA_TYPE='float' then 'float'
                            when DATA_TYPE='image' then 'byte[]'
                            when DATA_TYPE='int' then 'int'
                            when DATA_TYPE='numeric' then 'decimal'
                            when DATA_TYPE='decimal' then 'decimal'
                            when DATA_TYPE='double' then 'double'
                            when DATA_TYPE='real' then 'double'
                            when DATA_TYPE='smallint' then 'short'
                            when DATA_TYPE='mediumint' then 'int'
                            when DATA_TYPE='text' then 'string'
                            when DATA_TYPE='mediumtext' then 'string'
                            when DATA_TYPE='longtext' then 'string'                        
                            when DATA_TYPE='time' then 'TimeSpan'                        
                            when DATA_TYPE='timestamp' then 'DateTime'                        
                            when DATA_TYPE='tinyint' and COLUMN_TYPE = 'tinyint(1)'  then 'bool'
                            when DATA_TYPE='tinyint' then 'byte'
                            when DATA_TYPE='varbinary' then 'byte[]'
                            when DATA_TYPE='varchar' then 'string'
                            when DATA_TYPE='year' THEN 'uint'
                            else 'UNKNOWN_' + DATA_TYPE
                        end AS ColumnType
                        FROM ( SELECT  COLUMN_NAME, DATA_TYPE, COLUMN_TYPE,COLUMN_COMMENT,IS_NULLABLE  FROM INFORMATION_SCHEMA.COLUMNS  WHERE table_schema=pDataBaseName and table_name = pTableName) A);

    set vClassCode = '';
        
        -- 打開游標
    OPEN code_cursor;

            get_code: LOOP

                FETCH code_cursor INTO  colComment,colType,fName;

                IF v_finished = 1 THEN
                    LEAVE get_code;
                END IF;
                                
                                -- 處理字段
                                SELECT REPLACE(GROUP_CONCAT(CONCAT(UPPER(LEFT(A.Field,1)),SUBSTRING(A.Field,2,(LENGTH(A.Field)-1)))),',','')
                                FROM 
                                ( SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(fName,'_',help_topic_id+1),'_',-1) AS Field 
                                        FROM mysql.help_topic 
                                        WHERE help_topic_id < LENGTH(fName)-LENGTH(REPLACE(fName,'_',''))+1 ) A into fNameNew;

                -- 組合代碼字符串
                select  CONCAT(vClassCode,'\r\n\r\n', concat( '/// <summary>\r\n','/// ',colComment,'\r\n/// </summary>\r\n', 'public ', colType , ' ' ,fNameNew,' { get; set; }')) 
                                into  vClassCode ;

            END LOOP get_code;

        CLOSE code_cursor;

        -- 刪除臨時表
        drop table temp1;
    
        -- 最終生成類
        select concat('public class ',vClassName,'\r\n{', vClassCode,'\r\n}') as ModelClass;
END

 

若表名、字段名的命名規則不同或有特殊生成規則,則需進行相應的修改。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM