我的上个随笔提到我的代码生成器,已经可以生成用SQL语句操作数据库库,用存储过程操作数据库(并同时生成存储过程),和对数据进行映射。

我一直在努力想将他扩展成多种格式,语言,多数据库支持的。但我发现每种新格式必须新写不少代码,麻烦,而且格式只能写死在代码里。

大前天,我突然想到,为什么不设置一个格式文件通过解析格式生成代码呢,这样用户在拓展生成格式的时候只需要写个格式文件就可以了,跨语言,跨格式一步实现。

实现如下:

标识
<<TABLE_NAME>>         表名
<<U_TABLE_NAME>> 全部大写的表名
<<L_TABLE_NAME>> 全部小写的表名
<<UF_TABLE_NAME>> 首字母大写的表名
<<LF_TABLE_NAME>>       首字母小写的表名
<<COLUMN_NAME>>         列名
<<U_COLUMN_NAME>> 全部大写的列名
<<L_COLUMN_NAME>> 全部小写的列名
<<UF_COLUMN_NAME>> 首字母大写的列名
<<LF_COLUMN_NAME>>      首字母小写的列名
<<COLUMN_SIZE>>         列长度
<<COLUMN_TYPE>>         列类型
<<COLUMN_VALUE_TYPE>>   对应的数据类型
<<COLUMN_DB_Type>> 对应的数据库参数类型


命令
<<FOREACH_COLUMN>> <<END_FOREACH_COLUMN>> 在所有的字段循环
<<FOREACH_PKCOLUMN>>  <<END_FOREACH_PKCOLUMN>> 在所有的关键字段循环
<<REMOVE_COMMA>> <<END_REMOVE_COMMA>>  去掉最后的逗号

下面是我写的一个格式

 

using System;
using System.Data;
using System.Data.SqlClient;

namespace 命名空间
{
    
/// </summary>
    
///注释
    
/// </summary>

    public class <<TABLE_NAME>>DB
    
{
        
private SqlConnection conn=new SqlConnection();
        
private SqlCommand command=new SqlCommand();

        
public <<TABLE_NAME>>DB()
        
{
            conn.ConnectionString
=ConStrLib.GetConStr();
        }


        
<<FOREACH_COLUMN>>
        
private <<COLUMN_VALUE_TYPE>> <<LF_COLUMN_NAME>>;
        
public <<COLUMN_VALUE_TYPE>> <<UF_COLUMN_NAME>>
        
{
            
get
            
{
                
return <<LF_COLUMN_NAME>>;
            }

            
set
            
{
                
<<LF_COLUMN_NAME>>= value ;
            }

        }


        
<<END_FOREACH_COLUMN>>

        
        
public bool Add()
        
{
            
string strSql = "insert into <<TABLE_NAME>>
            (<<REMOVE_COMMA>><<FOREACH_COLUMN>><<COLUMN_NAME>>,<<END_FOREACH_COLUMN>><<END_REMOVE_COMMA>>)"
                +"values(@AdminID, @PassWord)" ;
            SqlCommand command 
= new SqlCommand(strSql,conn) ;

            
<<FOREACH_COLUMN>>
            command.Parameters.Add(
"@<<COLUMN_NAME>>",<<COLUMN_DB_Type>>) ;
            command.Parameters[
"@<<COLUMN_NAME>>"].Value = <<LF_COLUMN_NAME>>;
            
<<END_FOREACH_COLUMN>>

            
try
            
{
                conn.Open() ;
                command.ExecuteNonQuery() ;
                
return true ;
            }

            
catch(Exception e)
            
{
                
throw(new Exception("Error in the Database"+e.Message)) ;
            }

            
finally
            
{
                conn.Close() ;
            }

        }


        
public bool Modify()
        
{
            
string strSql ="update <<TABLE_NAME>> set <<REMOVE_COMMA>><<FOREACH_COLUMN>><<COLUMN_NAME>> = 

@
<<COLUMN_NAME>>,<<END_FOREACH_COLUMN>><<END_REMOVE_COMMA>> "
                + " where <<REMOVE_COMMA>><<FOREACH_PKCOLUMN>><<COLUMN_NAME>> = 

@
<<COLUMN_NAME>>,<<END_FOREACH_PKCOLUMN>><<END_REMOVE_COMMA>>";
        
            SqlCommand command 
= new SqlCommand(strSql,conn) ;
        
            
<<FOREACH_COLUMN>>
            command.Parameters.Add(
"@<<COLUMN_NAME>>",<<COLUMN_DB_Type>>) ;
            command.Parameters[
"@<<COLUMN_NAME>>"].Value = <<LF_COLUMN_NAME>>;
            
<<END_FOREACH_COLUMN>>
        
            
try
            
{
                conn.Open() ;
                command.ExecuteNonQuery() ;
                
return true ;
            }

            
catch(Exception e)
            
{
                
throw(new Exception("Error in the Database"+e.Message)) ;
            }

            
finally
            
{
                conn.Close() ;
            }

        }


        
public bool Delete()
        
{
            
string strSql ="delete from <<TABLE_NAME>>"
                
+ " where <<REMOVE_COMMA>><<FOREACH_PKCOLUMN>><<COLUMN_NAME>> = 

@
<<COLUMN_NAME>>,<<END_FOREACH_PKCOLUMN>><<END_REMOVE_COMMA>>";
        
            SqlCommand command 
= new SqlCommand(strSql,conn) ;
        
            
<<FOREACH_PKCOLUMN>>
            command.Parameters.Add(
"@<<COLUMN_NAME>>",<<COLUMN_DB_Type>>) ;
            command.Parameters[
"@<<COLUMN_NAME>>"].Value = <<LF_COLUMN_NAME>>;
            
<<END_FOREACH_PKCOLUMN>>
        
            
try
            
{
                conn.Open() ;
                command.ExecuteNonQuery() ;
                
return true ;
            }

            
catch(Exception e)
            
{
                
throw(new Exception("Error in the Database"+e.Message)) ;
            }

            
finally
            
{
                conn.Close() ;
            }

        }

生成的代码如下

using System;
using System.Data;
using System.Data.SqlClient;

namespace 命名空间
{
    
/// </summary>
    
///注释
    
/// </summary>

    public class AdminDB
    
{
        
private SqlConnection conn=new SqlConnection();
        
private SqlCommand command=new SqlCommand();

        
public AdminDB()
        
{
            conn.ConnectionString
=ConStrLib.GetConStr();
        }


        
        
private string adminID;
        
public string AdminID
        
{
            
get
            
{
                
return adminID;
            }

            
set
            
{
                adminID
= value ;
            }

        }


        
        
private string passWord;
        
public string PassWord
        
{
            
get
            
{
                
return passWord;
            }

            
set
            
{
                passWord
= value ;
            }

        }


        

        
        
public bool Add()
        
{
            
string strSql = "insert into Admin
            (AdminID,PassWord)"
                +"values(@AdminID, @PassWord)" ;
            SqlCommand command 
= new SqlCommand(strSql,conn) ;

            
            command.Parameters.Add(
"@AdminID",SqlDbType.Char) ;
            command.Parameters[
"@AdminID"].Value = adminID;
            
            command.Parameters.Add(
"@PassWord",SqlDbType.VarChar) ;
            command.Parameters[
"@PassWord"].Value = passWord;
            

            
try
            
{
                conn.Open() ;
                command.ExecuteNonQuery() ;
                
return true ;
            }

            
catch(Exception e)
            
{
                
throw(new Exception("Error in the Database"+e.Message)) ;
            }

            
finally
            
{
                conn.Close() ;
            }

        }


        
public bool Modify()
        
{
            
string strSql ="update Admin set AdminID = @AdminID,PassWord = @PassWord "
                
+ " where AdminID = @AdminID";
        
            SqlCommand command 
= new SqlCommand(strSql,conn) ;
        
            
            command.Parameters.Add(
"@AdminID",SqlDbType.Char) ;
            command.Parameters[
"@AdminID"].Value = adminID;
            
            command.Parameters.Add(
"@PassWord",SqlDbType.VarChar) ;
            command.Parameters[
"@PassWord"].Value = passWord;
            
        
            
try
            
{
                conn.Open() ;
                command.ExecuteNonQuery() ;
                
return true ;
            }

            
catch(Exception e)
            
{
                
throw(new Exception("Error in the Database"+e.Message)) ;
            }

            
finally
            
{
                conn.Close() ;
            }

        }


        
public bool Delete()
        
{
            
string strSql ="delete from Admin"
                
+ " where AdminID = @AdminID";
        
            SqlCommand command 
= new SqlCommand(strSql,conn) ;
        
            
            command.Parameters.Add(
"@AdminID",SqlDbType.Char) ;
            command.Parameters[
"@AdminID"].Value = adminID;
            
        
            
try
            
{
                conn.Open() ;
                command.ExecuteNonQuery() ;
                
return true ;
            }

            
catch(Exception e)
            
{
                
throw(new Exception("Error in the Database"+e.Message)) ;
            }

            
finally
            
{
                conn.Close() ;
            }

        }

哈哈,挺不错,现在我这个工具还在编码中,如果完成会马上通知大家,并公布代码。望多多指教。