程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#發現之旅第十二講 基於反射和動態編譯的快速ORM框架(下)(3)

C#發現之旅第十二講 基於反射和動態編譯的快速ORM框架(下)(3)

編輯:關於C語言

我們可以使用以下代碼來生成上述的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. 下一頁:
Copyright © 程式師世界 All Rights Reserved