Copy Code code as follows:
public bool Add (Liuyan Refmodel)
{
String sql = "INSERT into Liuyan (name,phone,zhiwei,gongsi,addr,country,dianyou,content,adddate) VALUES (@name, @phone, @zhiwei, @gongsi, @addr, @country, @dianyou, @content, @adddate) ";
oledbparameter[] Parameters ={
New OleDbParameter ("@name", OleDbType.VarChar),
New OleDbParameter ("@tel", OleDbType.VarChar),
New OleDbParameter ("@zhiwei", OleDbType.VarChar),
New OleDbParameter ("@gongsi", OleDbType.VarChar),
New OleDbParameter ("@addr", OleDbType.VarChar),
New OleDbParameter ("@country", OleDbType.VarChar),
New OleDbParameter ("@dianyou", OleDbType.VarChar),
New OleDbParameter ("@content", OleDbType.VarChar),
New OleDbParameter ("@adddate", OleDbType.VarChar)
};
Parameters[0]. Value=model. xingming;
PARAMETERS[1]. Value=model. Tel;
PARAMETERS[2]. Value=model. Zhiwei;
PARAMETERS[3]. Value=model. Gongsi;
PARAMETERS[4]. Value=model. ADDR;
PARAMETERS[5]. Value=model. Country;
PARAMETERS[6]. Value=model. Dianyou;
PARAMETERS[7]. Value=model. Content;
PARAMETERS[8]. Value=model. Adddate;
return Sqlhelper.executecommand (SQL, parameters);
}
This article speaks of reflection now say where in this piece of code the use of reflection is only in my coding process in order to Jingxiang whim
Copy Code code as follows:
Parameters[0]. Value=model. xingming;
PARAMETERS[1]. Value=model. Tel;
PARAMETERS[2]. Value=model. Zhiwei;
PARAMETERS[3]. Value=model. Gongsi;
PARAMETERS[4]. Value=model. ADDR;
PARAMETERS[5]. Value=model. Country;
PARAMETERS[6]. Value=model. Dianyou;
PARAMETERS[7]. Value=model. Content;
PARAMETERS[8]. Value=model. Adddate;
This code can use reflection to assign values when there are many attributes in the class.
Copy Code code as follows:
Use reflection to remember to reference a using System.Reflection namespace
Type t = Refmodel. GetType ();
for (int i = 0; i < t.getproperties (). Length; i++)
{
Parameters[i]. Value= t.getproperties () [i]. GetValue (Refmodel, NULL);
}
This uses reflection to get all the properties of the class
It then traverses the attribute and assigns the property value to the parameter
Of course here is the added method without model. Id
So the For loop starts at 1.
Then Parameters[i]. When value is assigned I want to subtract a parameters[i-1]. Value
So that when you write the data layer in the future, assign a value to the parameter without forcing a copy and paste
What about personal tips and sharing?
Using the Code generation tool can be ignored.