• [置顶] 第一次使用事物 利用线性表


    使用数据可事物可有效避免数据库当中的脏数据

    protected void btnSave_Click(object sender, EventArgs e)

            {
                Disasters d = new Disasters();
               
                d.GetModel(id);
                d.DisasterTitle = this.txtDisasterTitle.Text.Trim();
                d.DisasterDesc = this.txtDisasterDesc.Text.Trim();
                if (this.txtEventTime.Text.Trim() != "")
                {
                    d.EventTime = Convert.ToDateTime(this.txtEventTime.Text);
                }
                else
                {
                    d.EventTime = null;
                }
                if (this.txtLatitude.Text.Trim() != "")
                {
                    d.Latitude = Convert.ToDouble(this.txtLatitude.Text.Trim());
                }
                else
                {
                    d.Latitude = null;
                }
                if (this.txtLongitude.Text.Trim() != "")
                {
                    d.Longitude = Convert.ToDouble(this.txtLongitude.Text.Trim());
                }
                else
                {
                    d.Longitude = null;
                }
                if (this.txtMagnitude.Text.Trim() != "")
                {
                    d.Magnitude = Convert.ToDouble(this.txtMagnitude.Text);
                }
                else
                {
                    d.Magnitude = null;
                }
                if (this.txtRegion.Text.Trim() != "")
                {
                    d.Region = this.txtRegion.Text;
                }
                else
                {
                    d.Region = null;
                } if (this.txtDepth.Text.Trim() != "")
                {
                    d.Depth = Convert.ToDouble(this.txtDepth.Text.Trim());
                }
                else
                {
                    d.Depth = null;
                }
                d.AdminAuthFlag = 1;
                if (id > 0)
                {

                    if (!d.Exists(id))
                    {
                        this.lblMSG.Text = "保存失败";
                        return;
                    }
                }
                string  strSql="update disasters set "+
                    "DisasterTitle='"+d.DisasterTitle+"',"+"DisasterDesc='"+d.DisasterDesc+"',"+"Longitude='"+  d.Longitude+"',"+
                    "Latitude='"+d.Latitude+"',"+"Depth='"+d.Depth+"',"+"Magnitude='"+d.Magnitude+"',"+
                    "Region='"+ d.Region+"',"+"EventTime='"+d.EventTime+"' "+" where DisasterId='"+id+"',";
                List<String> SQLStringList=new List<String>();
                SQLStringList.Add(strSql);
                //遍历rptDisasters的选择项 更新AdminAuthFlag=-1 并将关联的图片 视频 disasterId更新为当前有效的DisasterId
                for (int i = 0; i < rptDisasters.Items.Count; i++)
                {

                    int disasterId= Convert.ToInt32(((HiddenField)rptDisasters.Items[i].FindControl("hidId")).Value);
                    CheckBox SelectThis = (CheckBox)rptDisasters.Items[i].FindControl("SelectThis");
                    if (SelectThis.Checked)
                    {
                        SQLStringList.Add("Update Videos set DisasterId='" + id + "' where DisasterId='" + disasterId + "'");
                        SQLStringList.Add("Update Pictures set DisasterId='" + id + "' where DisasterId='" + disasterId + "' ");
                        SQLStringList.Add("Update Texts set DisasterId='" + id + "' where DisasterId='" + disasterId + "'");
                        SQLStringList.Add("Update Disasters set AdminAuthFlag='-1' where DisasterId='" + disasterId + "'");

                        
                    }
                }
                //执行
                int rows = DbHelperMySQL.ExecuteSqlTran(SQLStringList);
                if (rows <= 0)
                {
                    this.lblMSG.Text = "保存失败";
                }

            }

    DbHelperMySQL 类中ExecuteSqlTran函数||                                 


     /// <summary>
            /// 执行多条SQL语句,实现数据库事务。
            /// </summary>
            /// <param name="SQLStringList">多条SQL语句</param>        
            public static int ExecuteSqlTran(List<String> SQLStringList)
            {
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    conn.Open();
                    MySqlCommand cmd = new MySqlCommand();
                    cmd.Connection = conn;
                    MySqlTransaction tx = conn.BeginTransaction();
                    cmd.Transaction = tx;
                    try
                    {
                        int count = 0;
                        for (int n = 0; n < SQLStringList.Count; n++)
                        {
                            string strsql = SQLStringList[n];
                            if (strsql.Trim().Length > 1)
                            {
                                cmd.CommandText = strsql;
                                count += cmd.ExecuteNonQuery();
                            }
                        }
                        tx.Commit();
                        return count;
                    }
                    catch
                    {
                        tx.Rollback();
                        return 0;
                    }
                }
            }

  • 相关阅读:
    二进制原码、反码、补码
    Activiti5.22.0扩展支持达梦数据库
    ABP token过期时间修改 放在appsettings.json
    巧用reduce 实现 checkbox 选中勾选联动
    用filter: dropshadow()给透明图片添加阴影
    MySQL安装后文本20220628
    记录U盘安装centos系统的 uefi的启动我过程
    openssl 生成自签证书及查看证书细节
    给脚本添加到环境变量_让你的脚本可以在任意地方都可执行的几个方法
    使用u盘安装centos7.2踩坑过程
  • 原文地址:https://www.cnblogs.com/wsq724439564/p/3258204.html
Copyright © 2020-2023  润新知