sql
CREATE PROCEDURE GetTableInfo
@TableName nvarchar ( 50 )
AS
BEGIN
SET NOCOUNT ON ;
select colname = sc.name,datatype = type_name(sc.user_type_id)
from sys.columns sc inner join sys.tables st
on sc. object_id = st. object_id
where st.name = @TableName
END
实体类模板
using System;
using System.Collections.Generic;
using System.Text;
using DA.Common;
namespace DA.BusinessEntities.TO
{
public class $TableName$:EntityBase
{
$TableColumn$
}
}
生成实体类代码
using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.IO;
/**/ /// <summary>
/// CreateClass 的摘要说明
/// </summary>
public class CreateClass
{
public static string CreateTo( string TableName)
{
string DataType = " string " ;
string TableColum = string .Empty;
string TableColums = string .Empty;
DataSet ds = GetTableInfo(TableName);
foreach (DataRow dr in ds.Tables[ 0 ].Rows)
{
DataType = GetDbType1(dr[ " datatype " ].ToString());
TableColum = " private " + DataType + " _ " + dr[ " colname " ].ToString().ToLower() + " ;\n\n " ;
TableColum += " public " + DataType + " " + dr[ " colname " ].ToString() + " \n{\n " ;
TableColum += " get { return _ " + dr[ " colname " ].ToString().ToLower() + " ; }\n " ;
TableColum += " set { _ " + dr[ " colname " ].ToString().ToLower() + " = value; }\n " ;
TableColum += " }\n\n " ;
TableColums += TableColum;
}
iHtml ihtml = new iHtml(Encoding.GetEncoding( " gb2312 " ));
ihtml.ReadHtmlModel(HttpContext.Current.Server.MapPath( " templete/to.txt " ));
ihtml.Replace( " $TableName$ " , TableName);
ihtml.Replace( " $TableColumn$ " , TableColums);
ihtml.WriteHtmlModel( @" D:\xxx\xxmysite\mysite\To\ " + TableName + " .cs " );
return ihtml.Content;
}
// 数据库的字段类型
private static string GetDbType1( string DataType)
{
switch (DataType.ToLower())
{
case " int " :
return " int " ;
break ;
case " datetime " :
return " DateTime " ;
break ;
default :
return " string " ;
break ;
}
}
// 返回dataset 表的字段和类型
public static DataSet GetTableInfo( string TableName)
{
Database db = DatabaseFactory.CreateDatabase( " FusionConnection " );
DbCommand cmd = db.GetStoredProcCommand( " GetTableInfo " );
db.AddInParameter(cmd, " TableName " , DbType.String, TableName);
return db.ExecuteDataSet(cmd);
}
}
public class iHtml
{
string strContent = string .Empty;
Encoding code = Encoding.GetEncoding( " gb2312 " );
public iHtml(Encoding Code)
{
code = Code;
}
操作文件 #region 操作文件
public bool ReadHtmlModel( string FilePath)
{
bool b = true ;
// 读取模板文件
StreamReader sr = null ;
try
{
sr = new StreamReader(FilePath, code);
strContent = sr.ReadToEnd(); // 读取文件
}
catch
{
sr.Close();
b = false ;
}
return b;
}
public bool WriteHtmlModel( string FilePath)
{
bool b = true ;
StreamWriter sw = null ;
// 写文件
try
{
sw = new StreamWriter(FilePath, false , code);
sw.Write(strContent);
sw.Flush();
}
catch
{
b = false ;
}
finally
{
sw.Close();
}
return b;
}
#endregion
替换操作 #region 替换操作
public void Replace( string Title, string Value)
{
strContent = strContent.Replace(Title, Value);
}
#endregion
public string Content
{
get
{
return strContent;
}
}
}
posted @
2008-06-05 15:49
zhengguoqing
阅读(
49 )
评论()
编辑
收藏
举报