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.搞定。。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章