Insert to C # Dynamic Database? [closed]

1

I am making a insert and I use a model with the Getters and Setters , when I do the insert in the values I get the model as a parameter and use the following way

  

sql = "INSERT INTO " + Table + "(" + campos + ") VALUES ('"+valores.nome+ "','" + valores.email + "', " + valores.cpf + ", " + valores.tel + ");";

Would I have to pass the Dynamic Values by taking it from model ?

    
asked by anonymous 20.03.2017 / 00:23

1 answer

1

I do not use a model, but I use System.Reflection.PropertyInfo to list the properties of an object and I am adding my syntax.

 foreach (System.Reflection.PropertyInfo pr in obj.GetType().GetProperties())
 {
     if (pr.CanRead)
     {
        object valor = pr.GetValue(obj, null);
     }
}

Then you just have a list of fields and the relationship between the columns to apply each value correctly to your place

    
20.03.2017 / 20:14