C # automatically extract the variable name with @ from SQL to obtain the generated sqlparameter array from the simple data object for data insertion
// Automatically obtain the @ variable in SQL, generate the sqlparameter array, and obtain the required variable value in SQL from a simple data object.
Public class zidonggetsqlpara
{
# Region Public static object [] getpararr (string strsql, object dataobj)
/// <Summary>
/// Use the sqlparameter array for database operation SQL
/// </Summary>
/// <Param name = "strsql"> SQL statement </param>
/// <Param name = "dataobj"> objects stored in variable data for SQL query </param>
/// <Returns> sqlparameter array </returns>
Public static object [] getpararr (string strsql, object dataobj)
{
Object [] retobj = new object [1];
// String strsql = "Update tablname set tb_password = @ tb_password where tb_password = @ oldtb_password and tb_username = @ tb_username where dd = @ DD and @ f_ff = dfffddd and @ Chinese field name = T chinese field name f and @ resr = @ etet222 ";
RegEx Rg = new RegEx ("@ [^, \ t/=) (;-] *", regexoptions. ignorecase );
Matchcollection matcol = RG. Matches (strsql );
Sqlparameter [] sqlpararr = new sqlparameter [matcol. Count];
Int I = 0;
Foreach (match MT in matcol)
{
Sqlpararr [I ++] = new sqlparameter (mt. Value, getfieldvalue (mt. value. tostring (). trimstart ('@'), dataobj ));
}
Retobj [0] = sqlpararr;
Return retobj;
}
# Endregion
# Region Public static string getfieldvalue (string strobjname, object dataobj) Get the variable or attribute name specified in the specified object
/// <Summary>
/// Get the variable or attribute name specified in the specified object
/// </Summary>
/// <Param name = "strobjname"> variable or attribute name </param>
/// <Param name = "dataobj"> Object </param>
/// <Returns> </returns>
Public static string getfieldvalue (string strobjname, object dataobj)
{
String strret = "";
Type T = dataobj. GetType ();
If (T. fullname. tostring (). Equals ("system. Collections. hashtable "))
{
Strret = convert. tostring (hashtable) dataobj) [strobjname]);
}
Else
{
Propertyinfo method_1 = T. getproperty (strobjname); // method name
If (method_1! = NULL)
{
Strret = convert. tostring (method_1.getgetmethod (). Invoke (dataobj, null); // obtain the attribute value
}
Else
{
Fieldinfo Fi = T. getfield (strobjname); // get the variable Field object
If (Fi! = NULL)
{
Strret = convert. tostring (Fi. getvalue (dataobj); // obtain the variable value
}
}
}
Return strret;
}
# Endregion
# Region Public static int datainsert (string strsql, string databeanobj) data insertion
///
// data insertion
///
/// insert SQL
// insert SQL to store objects with variables
//
Public static int datainsert (string strsql, object databeanobj)
{< br> int iret = 0;
sqlparameter [] sqlpararr = getpararr (strsql, databeanobj) [0] As sqlparameter [];
sqlhelper. executenonquery (sqlhelper. connectionstring, commandtype. text, strsql, sqlpararr);
return iret;
}< BR ># endregion
}
// Simple data object class
Public class logshow
{
// Pkid, Pcontent, createdate, errmesssge
Private string _ Pcontent;
Public String Pcontent
{
Get {return _ Pcontent ;}
Set {_ Pcontent = value ;}
}
Private string _ errmesssge;
Public String errmesssge
{
Get {return _ errmesssge ;}
Set {_ errmesssge = value ;}
}
}
// TestCode
Logshow logshowdatabean = new logshow ();
Logshowdatabean. errmesssge = "http://www.my400800.cn ";
Logshowdatabean. Pcontent = "http://www.my400800.cn is a good site. ";
Zidonggetsqlpara. datainsert ("insert into tb_log (Pcontent, errmesssge) values (@ Pcontent, @ errmesssge)", logshowdatabean );
// Reference to be added
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. reflection;
Using system. Data. sqlclient;
Using system. text;
Using system. Text. regularexpressions;
Using fjt. dbutility;
// Obtain the value of the SQL variable based on the simple data object
Zidonggetsqlpara. getfieldvalue ("strpri1", T1 );
Zidonggetsqlpara. getfieldvalue ("strpri2", T1 );
Zidonggetsqlpara. getfieldvalue ("strgetsettest", T1 );