這個例子主要的思路是建立一個類和數據庫查詢語句的字段結構是一致的
然後利用反射
namespace CCB_Donet
{
public class FieldRuleInfo
{
public string gStrFNo;
public string gStrFName;
public string gStrFLock;
public string gStrFCaption;
public string gStrFType;
public string gStrFMust;
public string gStrFMin;
public string gStrFMax;
public string gStrFDefault;
public string gStrFDate;
public string gStrFDB;
public string gStrFAllow;
public string gStrFDisallow;
public string gStrFSB;
public string gStrFBig;
public string gStrFSmall;
public string gStrFInputMethod;
public string gStrFCHK;
public string gStrFRelation;
public string gStrFDesc;
public string gStrFSecond;
public string gStrFQC;
public string gStrFException;
public string gStrFASupp;
public string gStrFYQH;
public string gStrFPos;
public string gStrFStar;
public string gStrFSave;
public string gStrFAddress;
public string gStrFLblColor;
public string gStrFIsCheckList;
}
}
#region 加載字段規則
private bool m_GetRule()
{
string strSQL = "";
DataTable dtGet = null;
#if(DEBUG)
try
{
#endif
if (Common
{
strSQL = "select A
"A
"A
"A
"A
"A
"INNER JOIN P_Field_Initial B ON A
"where A
"(B
}
else
{
strSQL = "select A
"A
"A
"A
"A
"A
"from P_Field_Rule A rn" +
"INNER JOIN P_Field_Initial B ON A
"where A
" AND B
" OR B
}
dtGet = DB
if (dtGet
{
Common
return false;
}
//獲得類信息
Type oType = Type
//生成類對象數組
mMainFieldRule = new FieldRuleInfo[dtGet
for (int i =
{
//這裡使用反射動態為FieldRuleInfo字段賦值數據
mMainFieldRule[i] = new FieldRuleInfo();
for (int j =
{
//這裡直接獲取類的字段名稱
FieldInfo fieldInfo = oType
BindingFlags
| BindingFlags
fieldInfo
}
}
return true;
#if(DEBUG)
}
catch (Exception ex)
{
return false;
MyLog
}
finally
{
dtGet = null;
}
#endif
}
#endregion
From:http://tw.wingwit.com/Article/program/net/201311/14047.html