我們可以使用以下代碼來生成上述的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(")");