If there are multiple column values in the database, but an indefinite number of updates is required to take "first by looking up all the data in the database into a model class, and then to the corresponding changes to the value of the copy, the other updated values to keep the original data unchanged"
For example: DAL (data access Layer)
/// <summary> ///update the data, although sometimes just update a value, the other values can be updated as is. Check the original data before updating, so as not to update the unnecessary updates/// </summary> /// <param name= "model" ></param> /// <returns></returns> Public intUpdata (Tblperson model) {stringsql ="UPDATE dbo. Tblperson Set [email protected],[email protected],[email protected],[email protected] where [email protected]"; Sqlparameter[] SPS=Newsqlparameter[] {NewSqlParameter ("@name", Model.uname),NewSqlParameter ("@age", Model.age),NewSqlParameter ("@height", model.height==NULL? DBNull.Value: (Object) model.height),NewSqlParameter ("@gender", model.gender==NULL? DBNull.Value: (Object) model.gender),NewSqlParameter ("@id", Model.autoid)}; returnsqlhelper.execurenonquery (Sql,commandtype.text,sps); } /// <summary> ///querying data for related items by autoid and returning the model class/// </summary> /// <param name= "autoid" ></param> /// <returns></returns> PublicTblperson Getmodelbyid (intautoid) { stringsql ="SELECT * from Tblperson where [email protected]"; Tblperson Model=NewTblperson (); using(SqlDataReader SDR = Sqlhelper.executereader (sql, CommandType.Text,NewSqlParameter ("@id", autoid))) { if(SDR). HasRows) {if(SDR). Read ()) {model.autoid= SDR. GetInt32 (0); Model.uname= SDR. GetString (1); Model.age= SDR. GetInt32 (2); Model.height= SDR. IsDBNull (3) ?NULL: (int?) Sdr. GetInt32 (3); Model.gender= SDR. IsDBNull (4) ?NULL: (BOOL?) Sdr. Getboolean (4); } } Else{model=NULL; } } returnmodel; }
BLL (business logic Layer)
Public int Updata (Tblperson model) { return dal. Updata (model); } Public Tblperson Getmodelbyid (int autoid) { return dal. Getmodelbyid (autoid); }
UI (Application layer)
Private voidButton1_Click (Objectsender, EventArgs e) {TBLPERSONBLL BLL=NewTBLPERSONBLL (); intID =Convert.ToInt32 (TextBox1.Text.Trim ()); Tblperson Model=BLL. Getmodelbyid (ID); Model.autoid=ID; Model.uname="Zhang San"; intR=BLL. Updata (model); MessageBox.Show ("Update"+r+"Line"); }
Data transfer problem when database is updated