日期:2011-06-15  浏览次数:20555 次

笔者近期在Vs2005平台和SqlServer2005平台之上研发了一个C#代码生成器,支持数据库表和C#数据对象的直接映射,以及CRUD操作代码的自动生成,用户只需要实现IDAL接口即可,该工具能够显著提高开发速度,让开发人员从繁重的数据库表对象封装编码中解脱出来(工具下载地址:http://itabby.com/index-5.asp),现特将核心处理代码进行发表,仅供参考:

///数据对象提取部分数据库类型和C#对象类型之间的转换过程
private string ConvertSqlDataReader(string sqlReader,string type, int i)
{
    switch (type.Trim().ToLower())
    {
 case "bigint": return sqlReader + ".GetInt64(" + i.ToString() + ")";
 ///case "binary": return sqlReader + ".GetValue(" + i.ToString() + ")";
 case "bit": return sqlReader + ".GetBoolean(" + i.ToString() + ")";
 case "char": return sqlReader + ".GetString(" + i.ToString() + ")";
 case "datetime": return sqlReader + ".GetDateTime(" + i.ToString() + ")";
 case "decimal": return sqlReader + ".GetDecimal(" + i.ToString() + ")";
 case "float": return sqlReader + ".GetFloat(" + i.ToString() + ")";
 ///case "image": return sqlReader + ".GetValue(" + i.ToString() + ")";
 case "int": return sqlReader + ".GetInt32(" + i.ToString() + ")";
 case "money": return sqlReader + ".GetDecimal(" + i.ToString() + ")";
 case "nchar": return sqlReader + ".GetString(" + i.ToString() + ")";
 case "ntext": return sqlReader + ".GetString(" + i.ToString() + ")";
 case "nvarchar": return sqlReader + ".GetString(" + i.ToString() + ")";
 case "real": return sqlReader + ".GetFloat(" + i.ToString() + ")";
 case "smalldatetime": return sqlReader + ".GetDateTime(" + i.ToString() + ")";
 case "smallint": return sqlReader + ".GetInt16(" + i.ToString() + ")";
 case "smallmoney": return sqlReader + ".GetDecimal(" + i.ToString() + ")";
 case "text": return sqlReader + ".GetString(" + i.ToString() + ")";
 case "timestamp": return sqlReader + ".GetDateTime(" + i.ToString() + ")";
 case "tinyint": return sqlReader + ".GetByte(" + i.ToString() + ")";
 case "udt": return sqlReader + ".GetValue(" + i.ToString() + ")";
 case "uniqueidentifier": return sqlReader + ".GetGuid(" + i.ToString() + ")";
 ///case "varbinary": return sqlReader + ".GetValue(" + i.ToString() + ")";
 case "varchar": return sqlReader + ".GetString(" + i.ToString() + ")";
 case "variant": return sqlReader + ".GetValue(" + i.ToString() + ")";
 case "xml": return sqlReader + ".GetString(" + i.ToString() + ")";
 case "numeric": return sqlReader + ".GetDecimal(" + i.ToString() + ")";
 default: throw new ArgumentException(type + " not in Itabby.Product.SqlCode.SqlCode.ConvertGetMethod process list.");
    }
}
///数据库类型完全限定名称还原
private string ConvertSqlDbType(string type)
{

    switch (type.Trim().ToLower())
    {
 case "bigint": return "System.Data.SqlDbType.BigInt";
 case "binary": return "System.Data.SqlDbType.Binary";
 case "bit": return "System.Data.SqlDbType.Bit";
 case "char": return "System.Data.SqlDbType.Char";
 case "datetime": return "System.Data.SqlDbType.DateTime";
 case "decimal": return "System.Data.SqlDbType.Decimal";
 case "float": return "System.Data.SqlDbType.Float";
 case "image": return "System.Data.SqlDbType.Image";
 case "int": return "System.Data.SqlDbType.Int";
 case "money": return "System.Data.SqlDbType.Money";
 case "nchar": return "System.Data.SqlDbType.NChar";
 case "ntext": return "System.Data.SqlDbType.NText";
 case "nvarchar": return "System.Data.SqlDbType.NVarChar";
 case "real": return "System.Data.SqlDbType.Real";
 case "smalldatetime": return "System.Data.SqlDbType.SmallDateTime";
 case "smallint": return "System.Data.SqlDbType.SmallInt";<