I am currently having trouble understanding why the output of the method below is different the second time it is called.
/// <summary>
/// Method which given an object will produce the sql string part.
/// </summary>
/// <typeparam name="T">Object type</typeparam>
/// <param name="modelObject">The object to assess</param>
/// <param name="param">true if appending @ (aka parameter)</param>
/// <returns>the string part of the sql.</returns>
public static string GetInsertParamString<T>(T modelObject, bool param = false)
{
//simple stringbuilder to hold the string we will return
StringBuilder str = new StringBuilder();
//loop through each of the proporties of the object
foreach (PropertyInfo prop in modelObject.GetType().GetProperties())
{
//if the value of this object is not null - then add to the string
if (modelObject.GetType().GetProperty(prop.Name)?.GetValue(modelObject, null) != null)
{
//if param == true then add the @ before.
str.Append(param ? "@" + prop.Name : prop.Name).Append(",");
}
}
//return the string.
return str.ToString().Remove(str.Length - 1, 1);
}
I am calling the method using the following line of C# code:
private static string InsertCrmSql<T>(T obj, string table) => @"INSERT INTO " + table + @" (" + SqlHelper.GetInsertParamString(obj) + @") VALUES (" + SqlHelper.GetInsertParamString(obj) + @")";
This demonstrates that the method is being called twice with the exact same parameter. NB: For debugging , I have removed the true value from the second call to ensure that this was not causing the problem.
When running In debug after the first call to the method the following is returned:
The final initial call string builder value clearer:
{wc_id,wc_status,wc_custref,wc_cuname,wc_transid,wc_transtype,wc_reqdate,wc_store,wc_salesman,wc_analysis1,wc_analysis2,wc_analysis3,wc_analysis4,wc_analysis5,wc_analysis6,wc_analysis7,wc_analysis8,wc_analysis9,}
However on the second call the following is found :
The final second call string builder value:
{wc_analysis1,wc_analysis2,wc_analysis3,wc_analysis4,wc_analysis5,wc_analysis6,wc_analysis7,wc_analysis8,wc_analysis9,wc_id,wc_status,wc_custref,wc_cuname,wc_transid,wc_transtype,wc_reqdate,wc_store,wc_salesman,}
Is anyone able to provide and insight or an explanation as to why this is happening?
I should also add its the same everytime the program is ran - the first result and then the second result
Cheers
Aucun commentaire:
Enregistrer un commentaire