Use the dynamic binding of the implementation front-end web window control and the background database class object

xiaoxiao2021-03-06  40

In the past, the change in the database field portion will occur during the final stage. If some fields have been added in a table, it is really a nightmare, see a few days in Microsoft's MSDN, about the front window control An example (http://www.microsoft.com/china/msdn/library/webservices/asp.net/ASPFORMBINDIND.MSPX), I used the reflection, I did test it when I used it. Efficiency can also be accepted, but because the code does not provide transaction logic and database class fields, I am a bit unfortunate, so I explain this part of me, explain it here, please take a lot of heroes. (Credits are published in http://mxjlb.ccwb.net/dbservice.rar )using system; use system.data; use system.reflection; useing system.configuration; namespace dbservice {///

/// Dataobjbinding Abstract. /// public class dataobjbinding {public dataobjbinding () {// // Todo: Add constructor logic here //}

Public static string [] getTableColum (String TableName) / / Write the field information of the table in Web.config. {

String [] strcolumn = configurationSettings.appsettings [Tablename] .tolower (). Split (','); Return strcolumn;}

///

/// BindDataToclassProperty summary description. /// Database table object is bundled to the attribute of the corresponding class object, and the data record is assigned to the corresponding object /// OBJ is the front desk class object /// TableName to query the table name /// Condition to query the condition / // public static void BindDataToClassProperty (object obj, string TableName, string Condition) {type objType = obj.GetType (); // reflect all attributes of the current class PropertyInfo [] objPropertiesArray = objType.GetProperties (); string Rowdata = ""; // 获得 获得 获得 数据 数据

DataRow daiRow = DbAccess.Select (TableName, GetTableColum (TableName), Condition) .Tables [0] .Rows [0]; foreach (DataColumn c in DbAccess.Select (TableName, GetTableColum (TableName), Condition) .Tables [0] .Columns) {rowdata = dairow [C.TOString ()]. ​​TOSTRING (); Foreach (PropertyInfo ObjProperty In objpropertiesArray) {// Check the matching name and type if (ObjProperty.Name.toupper () == C.toString ) .Toupper ()) {// Set the property of the control to the business object attribute value try {if (rowdata == "true") RowData = "1"; // If it is a true and false type if (rowdata == "FALSE ") RowData =" 0 "; ObjProperty.SetValue (Obj, rowdata, null); // Return True;} catch {// return false;}}} // Return False;} // return false;}

///

/// bindclasspropertytodata's summary description. /// The property of the corresponding class object is bundled to the database table object, and the data record is assigned to the corresponding object ////////////////////////////////////////////// Condition To query the condition / // public static void BindClassPropertyToData (object obj, string TableName, string Condition) {type objType = obj.GetType (); // reflect all attributes of the current class PropertyInfo [] objPropertiesArray = objType.GetProperties (); int Columnnnum = 0; // The current column serial number Object [] strvalue = new object [getTableColum (TABLENAME) .length]; // Initialize the object string to return in WebConfig (String C in gettablecolum (TABLENAME)) { Foreach (PropertyInfo ObjProperty In ObjProperties) {// Check the matching name and type if (ObjProperty.Name.toupper () == c.toString (). TouPper ()) {// Set the property to the business object property value Try {strvalue [columnnun] = objProperty.getValue (OBJ, NULL) .tostring (); // Return true;} catch}}} columnnu ; // return false;} DBACCESS.UPDATE (TABLENAME, GetTableColum (TableName), Strvalue, Condition;

///

/// bindclasspropertytodata's summary description. /// The property of the corresponding class object is bundled to the database table object, and the data record is assigned to the corresponding object ///////////////////////////////////// ' inserted /// /// public static void BindClassPropertyToData (object obj, string TableName, int Opera) {type objType = obj.GetType (); // reflect all attributes of the current class PropertyInfo [] objPropertiesArray = objType .Getproperties (); int columnnum = 0; // When the serial number of the current column Object [] strval = new object [getTableColum (TABLENAME) .length]; // Initialize the object string to return in WebConfig (String C in GetTableColum (TABLENAME)) {Foreach (PropertyInfo ObjProperty In ObjProperties) {// Check the matching name and type if (ObjProperty.Name.toupper () == C.TOString (). TouPper ()) {// Transmit control properties Set to business object attribute value try {strvalue [colorue [columnnnum] = objProperty.getValue (Obj, null) .tostring (); // return true;} catch {// return false;}}} columnnnum ; // Return False;} IF (Opera == 0) {strValue [0] = dbaccess.selectmaxid (TableName, TableName "ID"); dbaccess.inse RT (TableName, GetTableColum (TableName), Strvalue);}}}} This future If the database is changed, just use the value of the specified keyword in WebConfig, and modify the corresponding database class code is OK, no longer need to do Other modifications. My example of the database connection class has adopted an abstract type factory design pattern, and it is the advantage that everyone will know.

转载请注明原文地址:https://www.9cbs.com/read-60756.html

New Post(0)