首页
技术库|站长工具|技术手册|字体库|知识点词汇表| 联系我们|
打开本页的html静态页面
 

主菜单

文章分类

.: .Net技术 .: C#教程 .: c#写的代码生成器研究

  • 全文内容
  • 发表评论
  • 文章点评
  • 文章附件
  • Email文章
  • 打印文章

c#写的代码生成器研究

点击次数:447 创建日期:11-17-2007 录入:cn-web.com 字体:[ ] 点评:


重复的数据库操作,无休止的写存储过程参数列表,不停的对数据库的内容进行映射,无聊而且又容易出错,你是否已经厌倦了这样写代码。

其实这些代码的格式都几乎是固定的,完全可以自动生成。

我最近写了这么个小东东,希望能给大家一点帮助

思路路下

用OleDbConnection的GetOleDbSchemaTable方法获取数据库的机构存放在自己定义的对象中。

    //根据数据库获得数据库映射。
        public Schema GetSchema()
        

可能有人会问为什么使用OleDbConnection的GetOleDbSchemaTable方法,而不使用select * from sysobjects查询对象,我在这里考虑的是可以支持多种数据库平台,因为GetOleDbSchemaTable可以获得多种数据库的结构可以说是一劳永益,但这样做也会带来问题,因为得到的结构会使用同意的OleDbType来表示,在使用时必须转换到对应的SqlDBType和OracleDbType等,转换时类型可能不正确,在这里可以做了一些限制来保证转换的正确,

    case OleDbType.WChar:
                
{
                    
if (size<50)
                    
{
                        
return "SqlDbType.NChar";
                    }

                    
else if (size<1000)
                    
{
                        
return "SqlDbType.NVarChar";
                    }

                    
else
                    
{
                        
return "SqlDbType.NText";
                    }

                }

例如SqlServer数据库中的NChar,NVarchar和NText都被映射成OleDbType.WChar,所以我做出三个字段大小的一个限制来确保映射的正确性。

用类似如下代码生成相应的代码

        public override string BuildInsertFun(SchemaTable table) 
        

            StringBuilder sb 
= new StringBuilder() ; 
            sb.Append(
"public bool Add() ") ; 
            sb.Append(
"{ ") ; 
            sb.Append(
" ") ; 
            
string strInsert = ""insert into " + table.TableName + "("
            
string strFiledList = "" ; 
            
string strParamList = "" ; 
            
for(int i=0;i<table.Columns.Count;i++)
            
{
                
string strName = table.Columns[i].ColumnName ; 
                strFiledList 
= strFiledList + strName + "" ; 
                strParamList 
= strParamList + "@" + strName + "" ; 
            }

            strFiledList 
= strFiledList.Trim().TrimEnd(',') ; 
            strParamList 
= strParamList.Trim().TrimEnd(',') ; 
            sb.Append(
" string strSql = " + strInsert + strFiledList+ ")" ") ; 
            sb.Append(
" +"values(" + strParamList+ ")" ; ") ; 
            sb.Append(
" ") ; 
            sb.Append(
" SqlCommand command = new SqlCommand(strSql,conn) ; ") ; 
            sb.Append(
" ") ; 
            sb.Append(helper.BuildParameterString(table,
false,true));

            sb.Append(helper.AddCatchString()) ; 
   
            sb.Append(
"} ") ; 
            
return sb.ToString(); 
        }
 

这是一段生成的代码

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.Char) ;
            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.Char) ;
            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() ;
            }

        }

        
public DataSet GetAll()
        
{
            
string strSql ="select * from Admin";
            SqlDataAdapter da 
= new SqlDataAdapter(strSql,conn) ;
            DataSet ds 
= new DataSet() ;
        
            
try
            
{
                da.Fill(ds) ;
            }

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

            
finally
            
{
                da.Dispose() ;
            }

            
return ds ;
        }

        
public DataSet GetById()
        
{
            
string strSql ="select * from Admin"
                
+ " where AdminID=@AdminID";
        
            SqlDataAdapter da 
= new SqlDataAdapter(strSql,conn) ;
            DataSet ds 
= new DataSet() ;
        
            da.SelectCommand.Parameters.Add(
"@AdminID",SqlDbType.Char) ;