Data transfer problem when database is updated

Source: Internet
Author: User

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

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.