zoukankan      html  css  js  c++  java
  • 更新数据的经典代码

    private void UpdateUser()
            
    {
                
    if (Page.IsValid)
                
    {
                    
    // Update the existing user
                    SqlConnection con;
                    
    string sql;
                    SqlCommand cmd;
                    StringBuilder sb 
    = new StringBuilder();
                    ArrayList values 
    = new ArrayList();

                    
    // Build the SQL string
                    sb.Append("UPDATE [User] SET ");
                    sb.Append(
    "Login='{0}', Password='{1}', FirstName='{2}', ");
                    sb.Append(
    "LastName='{3}', PhoneNumber='{4}', Email='{5}'");

                    
    // Add required values to replace
                    values.Add(txtLogin.Text);
                    values.Add(txtPwd.Text);
                    values.Add(txtFName.Text);
                    values.Add(txtLName.Text);
                    values.Add(txtPhone.Text);
                    values.Add(txtEmail.Text);

                    
    // Add optional values directly
                    if (txtAddress.Text != string.Empty)
                        sb.Append(
    ", Address='" + txtAddress.Text + "'");

                    
    if (txtMobile.Text != string.Empty)
                        sb.Append(
    ", CellNumber='" + txtMobile.Text + "'");

                    
    if (txtBirth.Text != string.Empty)
                    
    {
                        
    // Pass date in ISO format YYYMMDD
                        DateTime dt = DateTime.Parse(txtBirth.Text);
                        sb.Append(
    ", DateOfBirth='");
                        sb.Append(dt.Year.ToString(
    "d4"));
                        sb.Append(dt.Month.ToString(
    "d2"));
                        sb.Append(dt.Day.ToString(
    "d2"));
                        sb.Append(
    "'");
                    }


                    sb.Append(
    " WHERE UserID='{6}'");

                    
    // Get the UserID from the context.
                    values.Add(Context.User.Identity.Name);
                    sql 
    = String.Format(sb.ToString(), values.ToArray());

                    
    // Connect and execute the query
                    con = new SqlConnection("data source=(local)\\NetSdk;initial catalog=FriendsData;user id=sa");
                    cmd 
    = new SqlCommand(sql, con);
                    con.Open();

                    
    bool doredirect = true;

                    
    try 
                    
    {
                        cmd.ExecuteNonQuery();
                    }
     
                    
    catch
                    
    {
                        doredirect 
    = false;
                        
    this.lblMessage.Visible = true;
                        
    this.lblMessage.Text = "Couldn't update your profile!";
                    }

                    
    finally
                    

                        con.Close();
                    }


                    
    if (doredirect)
                        Server.Transfer(
    "../Default.aspx");
                }

            }

  • 相关阅读:
    基于开源SuperSocket实现客户端和服务端通信项目实战
    WinForm基于插件开发实现多项配置存储
    WinForm多线程实现HTTP网络检测工具
    .NET开源分布式日志框架ExceptionLess实战演练(公开版)
    让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求
    .NET基于Eleasticsearch搭建日志系统实战演练
    Spring-Bean配置-使用外部属性文件(转)
    spring事务的隔离级别(透彻理解)
    Spring事务传播机制与隔离级别(转)
    SQL语句200条(转)
  • 原文地址:https://www.cnblogs.com/ahuang1118/p/172517.html
Copyright © 2011-2022 走看看