C# 快速生成Oracle数据库表的模型类

1.首选需要一款生成工具:CodeSmith(从网上搜索下载即可)

2.然后提供两个模板文件代码如下:

一个文件起名:Untitled2.cst

<%@ Register Name="Model" Template="D:\Work\BLBL\DOC\Untitled1.cst" MergeProperties="False" ExcludeProperties="" %>
 
<%@ Template Language="C#" TargetLanguage="Text" %>
<%@ Property Name="BaseNamespace" Type="String" %>
<%@ Property Name="OutPutDest" Type="String" %>
<%@ Property Name="DB" Type="SchemaExplorer.DatabaseSchema" DeepLoad="True" Optional="False" Category="Context" Description="" %>

<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Design" %>
<%@ Import Namespace="SchemaExplorer" %>

<%
CodeTemplate codeTemplate=new Model();
codeTemplate.SetProperty("BaseNamespace",BaseNamespace);
codeTemplate.SetProperty("SourceDatabase",DB);


%>
 
 
<%foreach(TableSchema tb in DB.Tables){
    codeTemplate.SetProperty("SourceTable",tb);

    codeTemplate.RenderToFile(OutPutDest+@"\"+tb.Name+".cs",true);

} 


%>

另一个文件起名:Untitled1.cst

<%-- 
Name:OracleModel
Author: 
Description: 根据指定的数据库生成业务逻辑类模板
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Description="根据指定的数据库表生成访问层类模板" ResponseEncoding="Unicode" %>
<%@ Property Name="NameSpace" Type="System.String" Default="TRACY.CG.Model"  Category="Context" Description="名称空间" %>
<%@ Property Name="DevelopersName" Type="String" Default="TRACY.CG.Model" Category="Context"  Description="开发人员姓名" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="数据库表名" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
///
// <%= GetDecimal(SourceTable) %>
// ---------------------
// Copyright <%= DevelopersName%> <%= DateTime.Now.Year %> Our Client
// ---------------------
// History
// <%= DateTime.Now.ToShortDateString() %>    
///
using System;
namespace <% =NameSpace %>
{
 /// <summary>
 /// <% =GetDecimal(SourceTable) %>  <%=SourceTable.ExtendedProperties["CS"].Value %>   
 /// </summary>
 [Serializable]
 public class <% =GetDecimal(SourceTable) %>
 {
  public <% =GetDecimal(SourceTable) %>()
  {}
  #region Model
  <% =GetVariables() %>
  
  <% =GetMethod() %>
  #endregion
  
  
 }
}
<script runat="template">
// My methods here.
//截取字符串前面的小数点只显示后面的字符串
public string GetDecimal(TableSchema table)
{
    return table.Name.Substring(table.Name.LastIndexOf(".")+1).ToString()+table.Description;
}

//判断数据类型是否为空
public string IsNull(ColumnSchema column)
{
 if(column.AllowDBNull) 
 {
  return "?";
 }
 return "";
}
//把字符串转换成小写
public string IsReplace(string str)
{
 return str.ToLower();
}
#region  拼装字符串
#region 拼装初始值
public string GetVariables()
{
 string str=string.Empty;
 
 for(int i=0;i<this.SourceTable.Columns.Count;i++)
 {
  if(IsNull(this.SourceTable.Columns[i])=="")
  {//数据类不型为空,数据类型不要加?
   str+=string.Format("private {0} _{1};",
   GetOracleDbType(this.SourceTable.Columns[i]),
   IsReplace(this.SourceTable.Columns[i].Name));
  }
  else
  {//为空时加问号
   if(GetOracleDbType(this.SourceTable.Columns[i])=="string")
   {//特殊情况数据类型是string类型不管为空,还是不能为空不能加?
    str+=string.Format("private {0} _{1};",
    GetOracleDbType(this.SourceTable.Columns[i]),
    IsReplace(this.SourceTable.Columns[i].Name));
   }
   else
   {//否则数据类型是非空类型后面带问号
    str+=string.Format("private {0}? _{1};",
    GetOracleDbType(this.SourceTable.Columns[i]),
    IsReplace(this.SourceTable.Columns[i].Name));
   }
  }
  
  if(i!=(this.SourceTable.Columns.Count-1))
  {
   str+="\r\n  ";
  }
 
 }
  
 return str;
}
#endregion
#region 拼装方法
public string GetMethod()
{
 string str=string.Empty;
 string a="{";
 string b="}";
 
 for(int i=0;i<this.SourceTable.Columns.Count;i++)
 {
  if(IsNull(this.SourceTable.Columns[i])=="")
  {//数据类不型为空,数据类型不要加?
   //拼装格式
   str+="/// <summary>\r\n  ";
   str+="/// "+SourceTable.Columns[i].Description+" \r\n  "; 
   str+="/// </summary>\r\n  ";
   
   str+=string.Format("public {0} {1}",
   GetOracleDbType(this.SourceTable.Columns[i]),
   this.SourceTable.Columns[i].Name);
   str+="\r\n  {\r\n   ";
   str+=string.Format("set{1} _{0} = value;{2}",IsReplace(this.SourceTable.Columns[i].Name),a,b);
   str+="\r\n   ";
   str+=string.Format("get{1}return _{0};{2}",IsReplace(this.SourceTable.Columns[i].Name),a,b);
   str+="\r\n  }";
  }
  else
  {//为空时加问号
   if(GetOracleDbType(this.SourceTable.Columns[i])=="string")
   {//特殊情况数据类型是string类型不管为空,还是不能为空不能加?
    //拼装格式
    str+="/// <summary>\r\n  ";
    str+="/// "+SourceTable.Columns[i].Description+" \r\n  "; 
    str+="/// </summary>\r\n  ";
    
    str+=string.Format("public {0} {1}",
    GetOracleDbType(this.SourceTable.Columns[i]),
    this.SourceTable.Columns[i].Name);
    str+="\r\n  {\r\n   ";
    str+=string.Format("set{1} _{0} = value;{2}",IsReplace(this.SourceTable.Columns[i].Name),a,b);
    str+="\r\n   ";
    str+=string.Format("get{1}return _{0};{2}",IsReplace(this.SourceTable.Columns[i].Name),a,b);
    str+="\r\n  }";
   }
   else
   {//否则数据类型是非空类型后面带问号
    //拼装格式
    str+="/// <summary>\r\n  ";
    str+="/// "+SourceTable.Columns[i].Description+" \r\n  "; 
    str+="/// </summary>\r\n  ";
    
    str+=string.Format("public {0}? {1}",
    GetOracleDbType(this.SourceTable.Columns[i]),
    this.SourceTable.Columns[i].Name);
    str+="\r\n  {\r\n   ";
    str+=string.Format("set{1} _{0} = value;{2}",IsReplace(this.SourceTable.Columns[i].Name),a,b);
    str+="\r\n   ";
    str+=string.Format("get{1}return _{0};{2}",IsReplace(this.SourceTable.Columns[i].Name),a,b);
    str+="\r\n  }";
   }
  }
  
  if(i!=(this.SourceTable.Columns.Count-1))
  {
   str+="\r\n  ";
  }
 
 }
 return str;
}
#endregion
#endregion
#region 判断数据类型

#region 根据列获取数据库的类型
///<summary>
///根据列获取数据库的类型
///<summary>
public string GetOracleDbType(ColumnSchema column)
{
 switch (column.NativeType)
 {
  case "Char": case "char": case "CHAR":
  case "varchar2": case "VarChar2": case "Varchar2": case "VARCHAR2":
  case "nchar": case "Nchar": case "NCHAR":
  case "nvarchar2": case "NVarChar2": case "NVARCHAR2":
  case "Long": case "LONG": case "long":
  case "Raw": case "RAW": case "raw":
  case "Long raw": case "LONG RAW": case "long raw":
  case "Rowid": case "rowid": case "ROWID":
  case "Blob": case "blob": case "BLOB":
  case "Clob": case "clob": case "CLOB":
  case "nclob": case "NCLOB": case "Nclob":
  case "Bfile": case "bfile": case "BFILE":
  case "Urowid": case "urowid": case "UROWID":
  return "string";
   
  case "number": case "Number": case "NUMBER":
  return "int";
  
  case "double": case "Double": case "DOUBLE":
  case "float": case "Float": case "FLOAT":
  return "decimal";
  
  case "DateTime": case "datetime": case "DATETIME":
  case "Date": case "date": case "DATE":
  return "DateTime";
  
  default: return "__UNKNOWN__" + column.NativeType; 
 }
}
#endregion
#endregion
</script>

然后使用工具CodeSmith打开这两个文件,

其中Untitled2.cst文件的第一行代码的 Template="D:\Work\BLBL\DOC\Untitled1.cst" 路径改为自己文件的路径下。

<%@ Register Name="Model" Template="D:\Work\BLBL\DOC\Untitled1.cst" MergeProperties="False" ExcludeProperties="" %>

 

然后注意Untitled1.cst文件里下面这两行的命名空间

Default="TRACY.CG.Model"

也改成自己想要的。

<%@ Property Name="NameSpace" Type="System.String" Default="TRACY.CG.Model"  Category="Context" Description="名称空间" %>
<%@ Property Name="DevelopersName" Type="String" Default="TRACY.CG.Model" Category="Context"  Description="开发人员姓名" %>

然后配置工具里的数据库连接字符串:

比如:data source=192.1.0.100:1521/orcl;persist security info=True;user id=abc;password=123456

选择好连接后,Test测试一下链接,成功后选择这个链接。

填写命名空间和输出目录:

然后在Untitled2.cst上面的Tools菜单下点击Generate生成

成功后目录里多出一个目录:

目录里就是数据库里的所有表模型文件了:

 

OK.搞定。。。。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值