实现SQL -> C# Class Code工具的问题

    有一段时间大家都在讨论生成SQL操作数据库代码的AutoCode工具,我也趁着热闹把我做的一个SQL AutoCode工具拿出来讨论,不过后来忘掉了这事,今天看见有人回复,希望把源码拿出来参考参考,索性就再说说我对制作这种工具的看法和遇到的问题。

    继续说明这个玩意儿不是OR Mapping,因为它只有Mapping而没有任何的Relation。我觉得非要取个名字叫Object DataRow Mapping还比较恰当。如果你在做数据库方面的内容,而且还在自己拼接SQL语句来更新数据库数据行的话,那么这篇文章将非常适合你的胃口。

    在各种各样的项目中,中小型项目中是数量众多的,很多这样的项目需要数据库,而又不会有十分复杂的表间依赖关系。在操作数据库是的代码也不是一成不变的,很多时候会有一些custom的内容需要加在代码里,想做一个大而全的AutoCode来通吃也非常的困难,我觉得更好的办法是把分解SQL语言的功能模块化,然后自己在项目中根具自己的需求来AutoCode就行了。

    在我的AutoCode里,我把每个数据库表的字段分解为:
None.gif      public   struct  SqlField
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public string Name;
InBlock.gif        
public SqlDbType DbType;
InBlock.gif        
public System.Type Type;
InBlock.gif        
public int Length;
InBlock.gif        
public string Sort;
InBlock.gif        
public bool IsNull;
ExpandedBlockEnd.gif    }

    分解的办法也非常的简单,就用正则表达式了:
None.gif string  strMatch  =   @" (\s*\[?(?<Name>\w+)\]?\s+\[?(?<DbType>\w+)\]?){1}(\s*\(?(?<Length>\d+(,\d+)?)\)?)?(\s+(?<Sort>COLLATE\s+\w+))?(\s+(?<IsNull>(NOT\s+)?NULL))?\s*\, " ;
None.gif
//                   ^---------------- Name + DbType ----------------^|<----------- Length ---------->|^---------- Sort ----------^|<--------- Is Null -------->|
None.gif
    // 在[宋体]下就能对齐这个pattern的这各部分的功能和其说明了 

    由于我的工具直接处理SQL语句,需要一个SqlType <-->CTS Type的映射表:
ContractedBlock.gif ExpandedBlockStart.gif SQL Type <--> CTS Type Mapping Table #region SQL Type <--> CTS Type Mapping Table
InBlock.gif    
// Sql data type mapping to .NET CTS type
InBlock.gif
    m_Type = new Hashtable();
InBlock.gif    m_Type[
"bigint"]    = "System.Int64";
InBlock.gif    m_Type[
"binary"]    = "System.Byte[]";
InBlock.gif    m_Type[
"bit"]        = "System.Boolean";
InBlock.gif    m_Type[
"char"]        = "System.String";        //object
InBlock.gif
    m_Type["datetime"]    = "System.DateTime";
InBlock.gif    m_Type[
"decimal"]    = "System.Decimal";
InBlock.gif    m_Type[
"float"]        = "System.Double";
InBlock.gif    m_Type[
"image"]        = "System.Byte[]";        //object
InBlock.gif
    m_Type["int"]        = "System.Int32";
InBlock.gif    m_Type[
"money"]        = "System.Decimal";
InBlock.gif    m_Type[
"nchar"]        = "System.String";        //object
InBlock.gif
    m_Type["ntext"]        = "System.String";        //object
InBlock.gif    
//m_Type["numeric"]    = "System.Decimal";
InBlock.gif
    m_Type["nvarchar"]    = "System.String";        //object
InBlock.gif
    m_Type["real"]        = "System.Single";
InBlock.gif    m_Type[
"smalldatetime"]    = "System.DateTime";
InBlock.gif    m_Type[
"smallint"]        = "System.Int16";
InBlock.gif    m_Type[
"smallmoney"]    = "System.Decimal";
InBlock.gif    m_Type[
"variant"]    = "System.Object";        //object
InBlock.gif
    m_Type["text"]        = "System.String";        //object
InBlock.gif
    m_Type["timestamp"]    = "System.Byte[]";        //object
InBlock.gif
    m_Type["tinyint"]    = "System.Byte";
InBlock.gif    m_Type[
"uniqueidentifier"]    = "System.Guid";//object
InBlock.gif
    m_Type["varbinary"]    = "System.Byte[]";        //object
InBlock.gif
    m_Type["varchar"]    = "System.String";        //object
ExpandedBlockEnd.gif
#endregion

    在SqlDataReader里面本来有一个映射表,
SqlDataReader.MetaData[int index].metaType.SqlType对应SqlDataReader.MetaData[int index].metaType.TypeName,可是MetaData属性不是public的,读不出来,郁闷。 

    通过遍历正则表达式的所有Match,获得一个SqlField数组:
ContractedBlock.gif ExpandedBlockStart.gif 生成SqlField数组的代码 #region 生成SqlField数组的代码
InBlock.gif            Match m 
= Regex.Match(strFields, strMatch, RegexOptions.IgnoreCase);
InBlock.gif            
while( m.Success )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SqlField sc 
= new SqlField();
InBlock.gif                sc.Name 
= m.Groups["Name"].Value;
InBlock.gif                
string strDbType = m.Groups["DbType"].Value.ToLower();
InBlock.gif                
if ( strDbType == "sql_variant" ) strDbType = "variant";
InBlock.gif                
if ( strDbType == "numeric" ) strDbType = "decimal";
InBlock.gif                sc.DbType 
= (SqlDbType)Enum.Parse(typEnum, strDbType, true);
InBlock.gif                
string strLength = m.Groups["Length"].Value;
InBlock.gif                
if ( strLength.Length == 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    sc.Length 
= 0;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sc.Length 
= int.Parse(m.Groups["Length"].Value);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sc.Length 
= 0;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                sc.Type 
= System.Type.GetType(m_Type[strDbType].ToString());
InBlock.gif                sc.Sort 
= m.Groups["Sort"].Value;
InBlock.gif                sc.IsNull 
= (m.Groups["IsNull"].Value.ToLower() == "null");
InBlock.gif                alstFields.Add(sc);
InBlock.gif                
//strSql = strSql.Replace(m.Value, "");
InBlock.gif
                m = m.NextMatch();
ExpandedSubBlockEnd.gif            }

InBlock.gif            m_Fields 
= (SqlField [])alstFields.ToArray(typeof(SqlField));
ExpandedBlockEnd.gif
#endregion

    上次我示例的代码就是我那次项目需要的代码而已,而有了SqlField数组后,要生成什么代码都巨方便了。下面这个Sql2Class.cs就是上次提到的那个 示例里的代码转换的源码。
ContractedBlock.gif ExpandedBlockStart.gif Sql2Class #region Sql2Class
InBlock.gif
using System;
InBlock.gif
using System.Collections;
InBlock.gif
using System.Text;
InBlock.gif
using System.Data;
InBlock.gif
using System.Data.SqlClient;
InBlock.gif
using System.Text.RegularExpressions;
InBlock.gif
InBlock.gif
namespace CodeConvert
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for SqlToCSharp.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Sql2Class
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private SqlFieldCollection m_SqlFields;
InBlock.gif
InBlock.gif        
public Sql2Class(string strSqlCode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_SqlFields 
= new SqlFieldCollection(strSqlCode);
InBlock.gif            CreateClass();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Sql2Class(SqlFieldCollection sfc)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_SqlFields 
= sfc;
InBlock.gif            CreateClass();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string CreateClass()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( m_SqlFields.FieldsCount == 0 ) return "";
InBlock.gif            StringBuilder strbCode 
= new StringBuilder();
InBlock.gif            strbCode.Append(
"/// <summary>\r\n/// Automatically Code for Table '");
InBlock.gif            strbCode.Append(m_SqlFields.TableName);
InBlock.gif            strbCode.Append(
"',\r\n/// by Birdshome AutoCode Engine V.1.0\r\n/// Copyright (C) 2004.1 Birdshome, HIT\r\n/// Create at : ");
InBlock.gif            strbCode.Append(DateTime.Now.ToString(
"yyyy-MM-dd hh:mm:ss"));
InBlock.gif            strbCode.Append(
"\r\n/// </summary>\r\npublic class ");
InBlock.gif            strbCode.Append(m_SqlFields.TableName);
InBlock.gif            strbCode.Append(
"\r\n{\r\n");
InBlock.gif            strbCode.Append(DefineVariables(m_SqlFields.Fields));
InBlock.gif            strbCode.Append(
"\r\n\tprivate static string m_TableName = \"");
InBlock.gif
            strbCode.Append(m_SqlFields.TableName);
InBlock.gif            strbCode.Append(
"\";\r\n\r\n");
InBlock.gif
            strbCode.Append(DefineAttributes(m_SqlFields.Fields));
InBlock.gif            strbCode.Append(
@"
InBlock.gif
    public " + m_SqlFields.TableName + @"()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        SqlCmd 
= new System.Data.SqlClient.SqlCommand();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public " + m_SqlFields.TableName + @"(int id) : this()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ( id <= 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new System.Exception(""The value of id must be greater than zero."");
ExpandedSubBlockEnd.gif        }

InBlock.gif        m_
" + m_SqlFields.Identity.Name + @" = id;
InBlock.gif        Load();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public " + m_SqlFields.TableName + @"(string field, string @value) : this()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ( field == null || field.Length == 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new System.NullReferenceException(""field"");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if ( @value == null || field.Length == 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new System.NullReferenceException(""@value"");
ExpandedSubBlockEnd.gif        }

InBlock.gif        Load(field, @value);
ExpandedSubBlockEnd.gif    }

InBlock.gif
");
InBlock.gif

InBlock.gif            strbCode.Append(
@"
InBlock.gif
    protected void Load()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ( m_" + m_SqlFields.Identity.Name + @" <= 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new System.Exception(""The value of m_ID must be greater than zero."");
ExpandedSubBlockEnd.gif        }

InBlock.gif        Load(m_
" + m_SqlFields.Identity.Name + @");
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
protected void Load(int id)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
string strSql = ""Select * From [dot.gif{0}] Where ([ID] = dot.gif{1})"";
InBlock.gif        RunSql(
string.Format(strSql, m_TableName, id));
ExpandedSubBlockEnd.gif    }

InBlock.gif
");
InBlock.gif

InBlock.gif            strbCode.Append(
@"
InBlock.gif
    protected void Load(string field, string @value)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ( @value.IndexOf('\'') != -1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            @value 
= @value.Replace(""'"", ""''"");
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
string strSql = ""Select * From [dot.gif{0}] Where ([dot.gif{1}= '{2}')"";
InBlock.gif        RunSql(
string.Format(strSql, m_TableName, field, @value));
ExpandedSubBlockEnd.gif    }

InBlock.gif
");
InBlock.gif

InBlock.gif            strbCode.Append(
@"
InBlock.gif
    private void RunSql(string strSql)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        SqlCmd.Connection 
= GetSqlConnection();
InBlock.gif        SqlCmd.CommandText 
= strSql;
InBlock.gif        System.Data.SqlClient.SqlDataReader drFields;
InBlock.gif        drFields 
= SqlCmd.ExecuteReader();
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ReadData(drFields);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch(System.Exception exp)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw exp;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            drFields.Close();
InBlock.gif            drFields 
= null;
InBlock.gif            SqlCmd.Connection.Close();
InBlock.gif            SqlCmd.Parameters.Clear();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
");
InBlock.gif

InBlock.gif            strbCode.Append(GetReadDataFunction(m_SqlFields.Fields).Replace(
"\r\n""\n").Replace("\n""\r\n"));
InBlock.gif            strbCode.Append(
"\r\n\t/// <summary>\r\n\t/// Save datas\r\n\t/// </summary>\r\n\tpublic void Save()\r\n\t{\r\n\t\tSave(m_");
InBlock.gif            strbCode.Append(m_SqlFields.Identity.Name);
InBlock.gif            strbCode.Append(
");\r\n\t}\r\n");
InBlock.gif            strbCode.Append(GetSaveFunction(m_SqlFields.Fields));
InBlock.gif            strbCode.Append(
"\r\n");
InBlock.gif            strbCode.Append(GetSaveAsFunction(m_SqlFields.Fields));
InBlock.gif            strbCode.Append(
"\r\n");
InBlock.gif            strbCode.Append(GetCopyToFunction(m_SqlFields.Fields));
InBlock.gif            strbCode.Append(
"\r\n\r\n\t~");
InBlock.gif            strbCode.Append(m_SqlFields.TableName);
InBlock.gif            strbCode.Append(
"()\r\n\t{\r\n\t\tSqlCmd.Dispose();\r\n\t}\r\n\r\n\t/// <summary>\r\n\t/// Create and return the database connection\r\n\t/// </summary>\r\n\t/// <returns>the opened database connection</returns>\r\n\tprotected static System.Data.SqlClient.SqlConnection GetSqlConnection()\r\n\t{\r\n\t\tstring strConn = \"SqlConnectionString\";\r\n\t\tstrConn = System.Configuration.ConfigurationSettings.AppSettings[strConn];\r\n\t\tSystem.Data.SqlClient.SqlConnection SqlConn;\r\n\t\tSqlConn = new System.Data.SqlClient.SqlConnection(strConn);\r\n\t\tSqlConn.Open();\r\n\t\treturn SqlConn;\r\n\t}\r\n");
InBlock.gif            strbCode.Append(GetExtraFunctions());
InBlock.gif            strbCode.Append(
"\r\n}");
InBlock.gif            
return strbCode.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string DefineVariables(SqlField [] fields)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder strbVariables 
= new StringBuilder();
InBlock.gif            strbVariables.Append(
"\tprivate ");
InBlock.gif            strbVariables.Append(m_SqlFields.Identity.Type.ToString());
InBlock.gif            strbVariables.Append(
" m_");
InBlock.gif            strbVariables.Append(m_SqlFields.Identity.Name);
InBlock.gif            strbVariables.Append(
";\r\n");
InBlock.gif            
forint i=0 ; i < fields.Length ; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strbVariables.Append(
"\tprivate ");
InBlock.gif                strbVariables.Append((fields[i]).Type.ToString());
InBlock.gif                strbVariables.Append(
" m_");
InBlock.gif                strbVariables.Append((fields[i]).Name);
InBlock.gif                strbVariables.Append(
";\r\n");
ExpandedSubBlockEnd.gif            }

InBlock.gif            strbVariables.Append(
"\tprivate System.Data.SqlClient.SqlCommand SqlCmd;\r\n\r\n");
InBlock.gif            
forint i=0 ; i < fields.Length ; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strbVariables.Append(
"\tprivate bool bSet");
InBlock.gif                strbVariables.Append((fields[i]).Name);
InBlock.gif                strbVariables.Append(
"\t= false;\r\n");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return strbVariables.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string DefineAttributes(SqlField [] fields)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strName, strType;
InBlock.gif            StringBuilder strbAttributes 
= new StringBuilder();
InBlock.gif            strbAttributes.Append(
"\tpublic ");
InBlock.gif            strbAttributes.Append(m_SqlFields.Identity.Type.ToString());
InBlock.gif            strbAttributes.Append(
" " + m_SqlFields.Identity.Name);
InBlock.gif            strbAttributes.Append(
"\r\n\t{\r\n\t\tget\r\n\t\t{\r\n\t\t\treturn m_");
InBlock.gif            strbAttributes.Append(m_SqlFields.Identity.Name);
InBlock.gif            strbAttributes.Append(
";\r\n\t\t}\r\n\t}\r\n");
InBlock.gif            System.Type type;
InBlock.gif            
forint i=0 ; i < fields.Length ; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strName 
= (fields[i]).Name;
InBlock.gif                type 
= (fields[i]).Type;
InBlock.gif                strType 
= type.ToString();
InBlock.gif
InBlock.gif                strbAttributes.Append(
"\r\n\tpublic ");
InBlock.gif                strbAttributes.Append(strType);
InBlock.gif                strbAttributes.Append(
" ");
InBlock.gif                strbAttributes.Append(strName);
InBlock.gif                strbAttributes.Append(
"\r\n\t{\r\n\t\tget\r\n\t\t{\r\n\t\t\treturn m_");
InBlock.gif                strbAttributes.Append(strName);
InBlock.gif                strbAttributes.Append(
";\r\n\t\t}\r\n\t\tset\r\n\t\t{\r\n\t\t\t");
InBlock.gif                
InBlock.gif                
if ( type == typeof(System.Byte) 
InBlock.gif                    
|| type == typeof(System.Int16) 
InBlock.gif                    
|| type == typeof(System.Int32) 
InBlock.gif                    
|| type == typeof(System.Int64) 
InBlock.gif                    
|| type == typeof(System.Single) )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    strbAttributes.Append(
"if ( m_");
InBlock.gif                    strbAttributes.Append(strName);
InBlock.gif                    strbAttributes.Append(
" == 0 || m_");
InBlock.gif                    strbAttributes.Append(strName);
InBlock.gif                    strbAttributes.Append(
" != value )\r\n\t\t\t{\r\n\t\t\t\tm_");
InBlock.gif                    strbAttributes.Append(strName);
InBlock.gif                    strbAttributes.Append(
" = value;\r\n\t\t\t\tbSet");
InBlock.gif                    strbAttributes.Append(strName);
InBlock.gif                    strbAttributes.Append(
" = true;\r\n\t\t\t}\r\n");
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
if ( type == typeof(System.Byte[]) 
InBlock.gif                    
|| type == typeof(System.Object) 
InBlock.gif                    
|| type == typeof(System.String) )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    strbAttributes.Append(
"if ( value != null && m_");
InBlock.gif                    strbAttributes.Append(strName);
InBlock.gif                    strbAttributes.Append(
" != value )\r\n\t\t\t{\r\n\t\t\t\tm_");
InBlock.gif                    strbAttributes.Append(strName);
InBlock.gif                    strbAttributes.Append(
" = value;\r\n\t\t\t\tbSet");
InBlock.gif                    strbAttributes.Append(strName);
InBlock.gif                    strbAttributes.Append(
" = true;\r\n\t\t\t}\r\n");
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
if ( type == typeof(System.Boolean) 
InBlock.gif                    
|| type == typeof(System.DateTime) 
InBlock.gif                    
|| type == typeof(System.Decimal) 
InBlock.gif                    
|| type == typeof(System.Double) 
InBlock.gif                    
|| type == typeof(System.Guid) )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    strbAttributes.Append(
"m_");
InBlock.gif                    strbAttributes.Append(strName);
InBlock.gif                    strbAttributes.Append(
" = value;\r\n\t\t\tbSet");
InBlock.gif                    strbAttributes.Append(strName);
InBlock.gif                    strbAttributes.Append(
" = true;\r\n");
ExpandedSubBlockEnd.gif                }

InBlock.gif                strbAttributes.Append(
"\t\t}\r\n\t}\r\n");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return strbAttributes.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string GetReadDataFunction(SqlField [] fields)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder strbReadData 
= new StringBuilder();
InBlock.gif            strbReadData.AppendFormat(
@"
InBlock.gif
    private void ReadData(System.Data.SqlClient.SqlDataReader drFields)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{dot.gif{
InBlock.gif        
bool bLoadSuccess = false;
InBlock.gif        
if ( drFields.Read() )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
object objdot.gif{0} = drFields[""dot.gif{0}""];
ExpandedSubBlockStart.gifContractedSubBlock.gif            m_
dot.gif{0} = (System.Int32)objdot.gif{0};
InBlock.gif
", m_SqlFields.Identity.Name);
InBlock.gif

InBlock.gif            
string strName;
InBlock.gif            
forint i=0 ; i < fields.Length ; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strName 
= (fields[i]).Name;
InBlock.gif                strbReadData.AppendFormat(
@"
ExpandedSubBlockStart.gifContractedSubBlock.gif
            object objdot.gif{0} = drFields[""dot.gif{0}""];
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ( !( objdot.gif{0} is System.DBNull ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                m_
dot.gif{0} = (dot.gif{1})objdot.gif{0};
ExpandedSubBlockEnd.gif            }
}
", strName, (fields[i]).Type.ToString());
ExpandedSubBlockEnd.gif
            }

InBlock.gif            strbReadData.Append(
@"
InBlock.gif
            bLoadSuccess = true;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
bool bNonUnique = drFields.Read();
InBlock.gif        
if ( bNonUnique || !bLoadSuccess )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strMessage = ""The identity isn't unique."";
InBlock.gif
            throw new System.Exception(strMessage);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
");
InBlock.gif
            return strbReadData.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string GetSaveFunction(SqlField [] fields)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder strbSave 
= new StringBuilder();
InBlock.gif            strbSave.Append(
"\r\n\t/// <summary>\r\n\t/// Save the datas of row which ID equal iIdentity.\r\n\t/// </summary>\r\n\t/// <param name=\"iIdentity\"></param>\r\n\tprivate void Save(int iIdentity)\r\n\t{\r\n\t\tif ( iIdentity <= 0 )\r\n\t\t{\r\n\t\t\tSaveAs();\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tSystem.Text.StringBuilder strbSql;\r\n\t\tstrbSql = new System.Text.StringBuilder();\r\n\t\tstrbSql.Append(\"Update [\" + m_TableName + \"] Set\");\r\n\t\tSystem.Data.SqlClient.SqlParameterCollection spc;\r\n\t\tspc = SqlCmd.Parameters;\r\n");
InBlock.gif            
string strName;
InBlock.gif            
forint i=0 ; i < fields.Length ; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SqlField sc 
= fields[i];
InBlock.gif                strName 
= sc.Name;
InBlock.gif                strbSave.Append(
"\t\tif ( bSet");
InBlock.gif                strbSave.Append(strName);
InBlock.gif                strbSave.Append(
" )\r\n\t\t{\r\n\t\t\tstrbSql.Append(\", [");
InBlock.gif
                strbSave.Append(strName);
InBlock.gif                strbSave.Append(
"] = @");
InBlock.gif                strbSave.Append(strName);
InBlock.gif                strbSave.Append(
"\");\r\n\t\t\tspc.Add(\"@");
InBlock.gif                strbSave.Append(strName);
InBlock.gif                strbSave.Append(
"\", System.Data.SqlDbType.");
InBlock.gif
                strbSave.Append(sc.DbType.ToString());
InBlock.gif                
if ( sc.Length > 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    strbSave.Append(
"");
InBlock.gif                    strbSave.Append(sc.Length);
ExpandedSubBlockEnd.gif                }

InBlock.gif                strbSave.Append(
");\r\n\t\t\tspc[\"@");
InBlock.gif
                strbSave.Append(strName);
InBlock.gif                strbSave.Append(
"\"].Value = m_");
InBlock.gif
                strbSave.Append(strName);
InBlock.gif                strbSave.Append(
";\r\n\t\t\tbSet");
InBlock.gif                strbSave.Append(strName);
InBlock.gif                strbSave.Append(
" = false;\r\n\t\t}\r\n");
ExpandedSubBlockEnd.gif            }

InBlock.gif            strbSave.Append(
"\t\tif( spc.Count > 0 )\r\n\t\t{\r\n\t\t\tspc.Add(\"@");
InBlock.gif
            strbSave.Append(m_SqlFields.Identity.Name);
InBlock.gif            strbSave.Append(
"\", System.Data.SqlDbType.");
InBlock.gif
            strbSave.Append(m_SqlFields.Identity.DbType.ToString());
InBlock.gif            strbSave.Append(
");\r\n\t\t\tspc[\"@");
InBlock.gif
            strbSave.Append(m_SqlFields.Identity.Name);
InBlock.gif            strbSave.Append(
"\"].Value = m_");
InBlock.gif
            strbSave.Append(m_SqlFields.Identity.Name);
InBlock.gif            strbSave.Append(
";\r\n\t\t\tstrbSql.Append(\" Where ([");
InBlock.gif
            strbSave.Append(m_SqlFields.Identity.Name);
InBlock.gif            strbSave.Append(
"] = @");
InBlock.gif            strbSave.Append(m_SqlFields.Identity.Name);
InBlock.gif            strbSave.Append(
")\");\r\n\t\t\tstrbSql.Replace(\" Set,\", \" Set \");\r\n\t\t\tSqlCmd.CommandText = strbSql.ToString();\r\n\t\t\tSqlCmd.Connection = GetSqlConnection();\r\n\t\t\tSqlCmd.ExecuteNonQuery();\r\n\t\t\tSqlCmd.Connection.Close();\r\n\t\t\tSqlCmd.Parameters.Clear();\r\n\t\t\tm_");
InBlock.gif
            strbSave.Append(m_SqlFields.Identity.Name);
InBlock.gif            strbSave.Append(
" = iIdentity;\r\n\t\t}\r\n\t}\r\n");
InBlock.gif            
return strbSave.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string GetSaveAsFunction(SqlField [] fields)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder strbSaveAs 
= new StringBuilder();
InBlock.gif            strbSaveAs.Append(
"\t/// <summary>\r\n\t/// Save the datas of current row to the new data row.\r\n\t/// </summary>\r\n\tpublic void SaveAs()\r\n\t{\r\n\t\tSystem.Data.SqlClient.SqlParameterCollection spc;\r\n\t\tspc = SqlCmd.Parameters;\r\n\t\tSystem.Text.StringBuilder strbValues;\r\n\t\tstrbValues = new System.Text.StringBuilder();\r\n");
InBlock.gif            
string strName;
InBlock.gif            
forint i=0 ; i < fields.Length ; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SqlField sc 
= fields[i];
InBlock.gif                strName 
= sc.Name;
InBlock.gif                strbSaveAs.Append(
"\r\n\t\tif ( bSet");
InBlock.gif                strbSaveAs.Append(strName);
InBlock.gif                strbSaveAs.Append(
" )\r\n\t\t{\r\n\t\t\tstrbValues.Append(\"@");
InBlock.gif
                strbSaveAs.Append(strName);
InBlock.gif                strbSaveAs.Append(
"\");\r\n\t\t\tspc.Add(\"@");
InBlock.gif                strbSaveAs.Append(strName);
InBlock.gif                strbSaveAs.Append(
"\", System.Data.SqlDbType.");
InBlock.gif
                strbSaveAs.Append(sc.DbType.ToString());
InBlock.gif                
if ( sc.Length > 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    strbSaveAs.Append(
"");
InBlock.gif                    strbSaveAs.Append(sc.Length);
ExpandedSubBlockEnd.gif                }

InBlock.gif                strbSaveAs.Append(
");\r\n\t\t\tspc[\"@");
InBlock.gif
                strbSaveAs.Append(strName);
InBlock.gif                strbSaveAs.Append(
"\"].Value = m_");
InBlock.gif
                strbSaveAs.Append(strName);
InBlock.gif                strbSaveAs.Append(
";\r\n\t\t\tbSet");
InBlock.gif                strbSaveAs.Append(strName);
InBlock.gif                strbSaveAs.Append(
" = false;\r\n\t\t}");
ExpandedSubBlockEnd.gif            }

InBlock.gif            strbSaveAs.Append(
@"
InBlock.gif
        string strFields, strValues;
InBlock.gif        
if ( strbValues.Length > 3 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlCmd.Connection 
= GetSqlConnection();
InBlock.gif            strValues 
= strbValues.ToString().Substring(2);
InBlock.gif            strFields 
= strbValues.Replace(""@""""], ["").ToString();
InBlock.gif            strFields 
= strFields.Substring(3);
InBlock.gif            SqlCmd.CommandText 
= ""Insert Into ["" + m_TableName + ""] ("" + strFields
InBlock.gif                
+ ""]) Values("" + strValues + "")\r\n Select SCOPE_IDENTITY() AS [SCOPE_IDENTITY]"";
InBlock.gif            
object obj = SqlCmd.ExecuteScalar();
InBlock.gif            
if ( obj == null || obj is System.DBNull )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception(""Save item failed."");
ExpandedSubBlockEnd.gif            }

InBlock.gif            m_
" + m_SqlFields.Identity.Name + @" = System.Convert.ToInt32(obj);
InBlock.gif            SqlCmd.Connection.Close();
InBlock.gif            SqlCmd.Parameters.Clear();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
");
InBlock.gif
            return strbSaveAs.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string GetCopyToFunction(SqlField [] fields)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder strbCopyTo 
= new StringBuilder();
InBlock.gif            strbCopyTo.Append(
"\t/// <summary>\r\n\t/// Copy the row to destination row which ID equal iDesID.\r\n\t/// </summary>\r\n\t/// <param name=\"iDesID\"></param>\r\n\tpublic void CopyTo(int iDesID)\r\n\t{\r\n");
InBlock.gif            
forint i=0 ; i < fields.Length ; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strbCopyTo.Append(
"\t\tbSet");
InBlock.gif                strbCopyTo.Append((fields[i]).Name);
InBlock.gif                strbCopyTo.Append(
"\t= true;\r\n");
ExpandedSubBlockEnd.gif            }

InBlock.gif            strbCopyTo.Append(
"\r\n\t\tSave(iDesID);\r\n\t}");
InBlock.gif            
return strbCopyTo.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string GetExtraFunctions()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strCode = @"
InBlock.gif
    private static int GetValue(string strSql)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.Data.SqlClient.SqlCommand SqlCmd;
InBlock.gif        SqlCmd 
= new System.Data.SqlClient.SqlCommand();
InBlock.gif        SqlCmd.Connection 
= GetSqlConnection();
InBlock.gif        SqlCmd.CommandText 
= strSql;
InBlock.gif        
object obj = SqlCmd.ExecuteScalar();
InBlock.gif        SqlCmd.Connection.Close();
InBlock.gif        
if ( obj == null || obj is System.DBNull )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return -1;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return (int)obj;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static int GetMaxID()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
string strSql = ""Select Max(ID) From [dot.gif{0}]"";
InBlock.gif        
return GetValue(string.Format(strSql, m_TableName));
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static bool IsValueExist(string strUnique, string strValue, int iExceptID)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.Data.SqlClient.SqlCommand SqlCmd;
InBlock.gif        SqlCmd 
= new System.Data.SqlClient.SqlCommand();
InBlock.gif        SqlCmd.Connection 
= GetSqlConnection();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
string strSql = ""Select Count(*) From [dot.gif{0}] Where ([dot.gif{1}= @Value)"";
InBlock.gif        strSql 
= string.Format(strSql, m_TableName, strUnique);
InBlock.gif        SqlCmd.Parameters.Add(
""@Value"", System.Data.SqlDbType.NVarChar).Value = strValue;
InBlock.gif        
if ( iExceptID >= 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            strSql 
= string.Format(""dot.gif{0} AND ([" + m_SqlFields.Identity.Name + @"<> '{1}')"", strSql, iExceptID);
ExpandedSubBlockEnd.gif        }

InBlock.gif        SqlCmd.CommandText 
= strSql;
InBlock.gif        
int iCount = (int)SqlCmd.ExecuteScalar();
InBlock.gif        SqlCmd.Connection.Close();
InBlock.gif        
return !(iCount == 0);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static bool IsValueExist(string strUnique, string strValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return IsValueExist(strUnique, strValue, -1);
ExpandedSubBlockEnd.gif    }
";
InBlock.gif
            return strCode;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Convert()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return CreateClass().Replace("\r\n""\n").Replace("\n""\r\n").Trim();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif
#endregion
    同时我也根据项目需要写了几个Sql2Aspx.cs和Sql2AspxCS.cs来生成表单代码,后面这两的通用性就更低了,不过都需要SqlField里的数据来支持转换生成。这些转换中也没有使用CodeDom,而完全使用字符串拼接,甚至直接一段一段的代码写里面,这样做就是为了需要有更改的时候方便,写成CodeDom方式那就完蛋了,时间长了要看半天才能找到要在什么地方做改动。

    关于这种Mapping的好处就是自己对数据库访问代码的可控性很强,有问题可以根据自己的需求马上改,而不用去适应一些并不在目前情况下使用的东西,效率也可以做到最好,同时由于代码是自动生成,也不会有太多的重复劳动。操作数据库条目也巨方便,IDE里能做到Sql Field的IntelliSence。 

     AutoCode下载,除了它的SQL转SqlField数组的功能外,其它代码都是为了一次项目而构建的,不必关注。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值