Using reflection to insert a database and update a database

Source: Internet
Author: User


public int insert<t> (T m) where T:class
{
propertyinfo[] Pinfos = M.gettype (). GetProperties ();
fieldinfo[] Finfos = M.gettype (). GetFields ();

list<string> Lstcol = new list<string> ();
list<oledbparameter> Lstparam = new list<oledbparameter> ();

for (int i = 0; i < pinfos.getlength (0); i++)
{
PropertyInfo pInfo = pinfos[i];
Object value = Pinfo.getvalue (M, null);
String name = Pinfo.name;
string declartype = PInfo.PropertyType.Name;
if (Declartype.toupper (). Contains ("DateTime"). ToUpper ()))
{
Value = ((DateTime) value). ToString ("Yyyy-mm-dd HH:mm:ss.fff");
}

Lstcol.add (name);
Lstparam.add (New OleDbParameter ("@" + name, value));
}

String Keys = "" + String. Join (",", Lstcol.toarray ());
String Values = "" + "@" + string. Join (", @", Lstcol.toarray ());


String sql;
sql = @ "Insert into" + gettablename () + "("
+ Keys
+ ") VALUES ("
+ Values
+ ")";

String errormsg;
return dbahelp.dba_executenonquery (SQL, (oledbparameter[]) (Lstparam.toarray ()), out errormsg);

}

public int update<t> (T m, string updatekey) where T:class
{
String FullName = M.gettype (). FullName;
T mnew = Activator.CreateInstance (M.gettype ()) as T;

propertyinfo[] Pinfos = M.gettype (). GetProperties ();
propertyinfo[] pinfosnew = Mnew.gettype (). GetProperties ();


list<string> Lstcol = new list<string> ();
list<oledbparameter> Lstparam = new list<oledbparameter> ();

OleDbParameter Keyparam = null;
String sKey = "";

for (int i = 0; i < pinfos.getlength (0); i++)
{
PropertyInfo pInfo = pinfos[i];
Object value = Pinfo.getvalue (M, null);
String name = Pinfo.name;

PropertyInfo pinfonew = Pinfosnew[i];
Object valuenew = Pinfonew.getvalue (mnew, NULL);

if (updatekey.toupper () = = name. ToUpper ())
{
Keyparam = new OleDbParameter ("@" + name, value);
SKey = name + "[email protected]" + name;
}
else if (!value. Equals (valuenew))
{
String line = name + "[email protected]" + name;
Lstcol.add (line);
Lstparam.add (New OleDbParameter ("@" + name, value));
}
}

String Lines = String. Join (",", Lstcol.toarray ());
if (Keyparam! = null)
{
Lstparam.add (Keyparam);
}

String sql;
sql = @ "Update" + gettablename () + "Set"
+ Lines
+ "Where"
+ SKey
+ "";

String errormsg;
return dbahelp.dba_executenonquery (SQL, (oledbparameter[]) (Lstparam.toarray ()), out errormsg);
}

Using reflection to insert a database and update a database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.