example
string insertquery = Myobject.ToSqlQuery();
put these functions in a public static class file:
public static string ToSqlQuery
{
int counter = 0;
string Columnstatement = "INSERT INTO " + typeof(T).Name + " (";
string ValueStatement = " VALUES ( ";
foreach (var pi in typeof(T).GetProperties())
{
string priValue = string.Empty;
PropertyInfo pil = (PropertyInfo)pi;
try
{
priValue = pi.GetGetMethod().Invoke(primary, null).ToString();
}
catch (Exception)
{
}
if (priValue != string.Empty)
{
if (counter > 0)
{
ValueStatement += ",";
Columnstatement += ",";
}
Columnstatement += "[" + pil.Name + "]";
ValueStatement += "'" + priValue + "'";
counter++;
}
}
return Columnstatement + ") " + ValueStatement + ")";
}
public static string ToSqlCreateTable
{
int counter = 0;
string Columnstatement = "CREATE TABLE " + "TABLENAME (";
string ValueStatement = "VALUES ( ";
foreach (var pi in typeof(T).GetProperties())
{
PropertyInfo pil = (PropertyInfo)pi;
if (counter > 0)
{
Columnstatement += ",";
}
Columnstatement += "[" + pil.Name + "]" + " VARCHAR(254)";
counter++;
}
Columnstatement += ")";
return Columnstatement;
}
Geen opmerkingen:
Een reactie posten