WEB开发网
开发学院软件开发C语言 C#发现之旅:基于反射和动态编译的快速ORM框架(下... 阅读

C#发现之旅:基于反射和动态编译的快速ORM框架(下)

 2010-09-30 21:08:22 来源:WEB开发网   
核心提示: 我们可以使用以下代码来生成上述的C#代码文本。//关键字段SQL参数名称列表System.Collections.ArrayListKeyParameterNames=newSystem.Collections.ArrayList();//生成Where子语句文本System.Text.Stri

我们可以使用以下代码来生成上述的C#代码文本。

// 关键字段SQL参数名称列表
System.Collections.ArrayList KeyParameterNames = new System.Collections.ArrayList();
// 生成Where子语句文本
System.Text.StringBuilder myWhereSQL = new System.Text.StringBuilder();
System.Collections.ArrayList KeyProperties = new System.Collections.ArrayList();
for (int iCount = 0; iCount < ps.Length; iCount++)
{
    System.Reflection.PropertyInfo p = ps[iCount];
    if (p.CanRead == false)
    {
        continue;
    }
    BindFieldAttribute fa = (BindFieldAttribute)Attribute.GetCustomAttribute(
        p, typeof(BindFieldAttribute));
    if (fa.Key == false)
    {
        continue;
    }
 
    string FieldName = this.GetBindFieldName(p);
    if (myWhereSQL.Length > 0)
    {
        myWhereSQL.Append(" and ");
    }
    KeyProperties.Add(p);
    if (bolNamedParameter)
    {
        string pName = "Key" + p.Name;
        KeyParameterNames.Add(pName);
        myWhereSQL.Append(FixFieldName(FieldName) + " = @" + pName + " ");
    }
    else
    {
        myWhereSQL.Append(FixFieldName(FieldName) + " = ? ");
    }
}//for
 
myWriter.WriteLine("public override int FillDeleteCommand( System.Data.IDbCommand cmd , object objRecord )");
myWriter.BeginGroup("{");
if (KeyProperties.Count == 0)
{
    myWriter.WriteLine("throw new NotSupportedException(\"FillDeleteCommand\");");
}
else
{
    myWriter.WriteLine("if( cmd == null ) throw new ArgumentNullException(\"cmd\");");
    myWriter.WriteLine("if( objRecord == null ) throw new ArgumentNullException(\"objRecord\");");
    myWriter.WriteLine(RecordType.FullName + " myRecord = objRecord as " + RecordType.FullName + " ;");
    myWriter.WriteLine("if( myRecord == null ) throw new ArgumentException(\"must type '" + RecordType.FullName + "' \");");
    System.Text.StringBuilder myDeleteSQL = new System.Text.StringBuilder();
    myDeleteSQL.Insert(0, "Delete From " + TableName + " Where " + myWhereSQL.ToString());
    myWriter.WriteLine("cmd.Parameters.Clear();");
    myWriter.WriteLine("cmd.CommandText = @\"" + myDeleteSQL.ToString() + "\" ;");
    myWriter.WriteLine("System.Data.IDbDataParameter parameter = null ;");
    int index = 0;
    foreach (System.Reflection.PropertyInfo p in KeyProperties)
    {
        myWriter.WriteLine("");
        myWriter.WriteLine("parameter = cmd.CreateParameter();");
        WriteSetParameterValue(p, myWriter);
        if (bolNamedParameter)
        {
            myWriter.WriteLine("parameter.ParameterName = \"" + KeyParameterNames[index] + "\";");
        }
        myWriter.WriteLine("cmd.Parameters.Add( parameter );");
        index++;
    }
    myWriter.WriteLine("");
    myWriter.WriteLine("return " + KeyProperties.Count + " ;");
 
}
 
myWriter.EndGroup(")");

上一页  1 2 3 4 5 6 7 8  下一页

Tags:发现 之旅 基于

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接