Here I created a generic class called Object which is supposed to be able to load / save / change any of my business objects. Each one of my business objects are inherited a generic class called Table, it possesses an attribute that most defines the name of the table and the primary key of the table base. Each of the properties have an attribute correspondent to the name and type of field base. That's the context.
My problem is with the backup. Whenever I want to save an object, I loop on the attribute and I get the name of fields and the associated value, I built my request like this.
The structure of the request is correct but the valuers are not replaced. For example, to type a query I will insert:
Code:
INSERT INTO test (id, value) values (@ID, @value)
It does not replace IDs parameter values.
Here is my code, I hope I have been quite clear:
Code:
public static void SaveObject (AGDDataTable poObject)
{
MySqlConnection cn;
MySqlParameter param;
MySqlCommand cmd;
String sSqlUpdate;
String sSqlInsertFields;
String sSqlInsertValues;
Type type = poObject.GetType ();
AGDTable atrTable = ((AGDTable []) type.GetCustomAttributes (typeof (AGDTable), true)) [0];
String sTableName = atrTable.TableName;
String sPrimaryKey = atrTable.PrimaryKey;
String sVirgule = "";
sSqlUpdate = "UPDATE " + sTableName + " SET ";
sSqlInsertFields = "INSERT INTO " + sTableName + "(" + sPrimaryKey;
sSqlInsertValues = "VALUES (@ ID";
foreach (PropertyInfo pi in type.GetProperties ()) / / Construction of the request from the properties in my class
{
AGDField [] aoField = (AGDField []) pi.GetCustomAttributes (typeof (AGDField), false);
if (aoField.Length> 0)
{
String sFieldName = aoField[0].FieldName;
sSqlUpdate += sVirgule + sFieldName + " = @" + sFieldName;
sSqlInsertFields += ", " + sFieldName;
sSqlInsertValues += ", @" + sFieldName;
sVirgule = ", ";
}
}
sSqlUpdate += " WHERE " + sPrimaryKey + " = @ID";
sSqlInsertFields += " )";
sSqlInsertValues += " )";
cn = new MySqlConnection(Properties.Resources.SECUConnectionString);
cn.Open ();
if (poObject.Id> 0)
{
cmd = new MySqlCommand (sSqlUpdate, cn);
}
else
{
poObject.Id = GetNextId(sTableName);
cmd = new MySqlCommand (sSqlInsertFields + sSqlInsertValues, cn);
}
foreach (PropertyInfo pi in type.GetProperties ()) / / Creating parameters
{
AGDField [] aoField = (AGDField []) pi.GetCustomAttributes (typeof (AGDField), false);
if (aoField.Length> 0)
{
/ / Get the attribute AGDField to know the name of the field and type in base
AGDField field = aoField [0];
param = new MySqlParameter (field.FieldName, field.DBType);
/ / Get the value of this property for this instance of an object
param.Value = pi.GetValue (poObject, null);
cmd.Parameters.Add (param);
}
}
param = new MySqlParameter (sPrimaryKey, MySqlDbType.Int64);
param.Value = poObject.Id;
cmd.Parameters.Add (param);
try
{
cmd.ExecuteNonQuery ();
}
catch (Exception excp)
{
Exception myExcp = new Exception("Could not Save " + poObject.GetType().Name + ". Error: " + excp.Message, excp);
throw (myExcp);
}
}
Do someone know why my settings are not taken into account?
Bookmarks