StringBuilder字符串拼接用法举例

来源:互联网 发布:数据库事务是什么 编辑:程序博客网 时间:2024/05/16 12:07
   private void tsbEdit_Click(object sender, EventArgs e)
        {
            int loadManQty = 0;
            StringBuilder sb = new StringBuilder();
            string transportID = txtID.Text.Trim();
            string loadDate = dateFyrq.Text.Trim();
            ProcSqlHelper.ExecuteNonQuery(@"update FyTransport set loadDate= @loadDate where transportID = @transportID", CommandType.Text, new SqlParameter[] { new SqlParameter("@loadDate", loadDate), new SqlParameter("@transportID", transportID) });
            string insertSql = @"insert into FytransportLoadman(transportID,loadmanID,name,chk) values (@transportID,@loadmanID,@name,@chk)";
            //string updSql = @"update FytransportLoadman set chk= @chk where transportID=@transportID and loadmanID = @loadmanID and name =@name";
            string delSql = @"delete from  FytransportLoadman where transportID=@transportID and loadmanID = @loadmanID and name =@name";
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                string loadmanID = dataGridView1.Rows[i].Cells["ID"].Value.ToString();
                string name = dataGridView1.Rows[i].Cells["name"].Value.ToString();
                if ((bool)dataGridView1.Rows[i].Cells["chk"].EditedFormattedValue == true)
                {
                    dataGridView1.Rows[i].Cells["chk"].Value = true;                 
                    bool chk = Convert.ToBoolean(dataGridView1.Rows[i].Cells["chk"].Value);
                    int r = Convert.ToInt32(ProcSqlHelper.ExecuteScalar(@"select count(*) from FytransportLoadman where transportID=@transportID and loadmanID = @loadmanID and name =@name ", CommandType.Text, new SqlParameter[] { new SqlParameter("@transportID", transportID), new SqlParameter("@loadmanID", loadmanID), new SqlParameter("@name", name) }));
                    if (r == 0)
                    {
                        ProcSqlHelper.ExecuteNonQuery(insertSql, CommandType.Text, new SqlParameter[] { new SqlParameter("@transportID", transportID), new SqlParameter("@loadmanID", loadmanID), new SqlParameter("@name", name), new SqlParameter("@chk", chk) });
                    }
                    loadManQty++;           
                    sb.Append(name + @",");
                }
                else
                {
                    int r = Convert.ToInt32(ProcSqlHelper.ExecuteScalar(@"select count(*) from FytransportLoadman where transportID=@transportID and loadmanID = @loadmanID and name =@name ", CommandType.Text, new SqlParameter[] { new SqlParameter("@transportID", transportID), new SqlParameter("@loadmanID", loadmanID), new SqlParameter("@name", name) }));
                    if (r > 0)
                    {
                        ProcSqlHelper.ExecuteNonQuery(delSql, CommandType.Text, new SqlParameter[] { new SqlParameter("@transportID", transportID), new SqlParameter("@loadmanID", loadmanID), new SqlParameter("@name", name)});
                    }
                }
            }
            if (sb.Length >= 1)
            {
                sb.Remove(sb.Length - 1, 1);
                string loadGroup = sb.ToString();
                for (int i = 0; i < MotherForm.dataGridView1.Rows.Count; i++)
                {
                    string ID = MotherForm.dataGridView1.Rows[i].Cells["ID"].Value.ToString();
                    if (ID == transportID)
                    {
                        MotherForm.dataGridView1.Rows[i].Cells["装卸人数"].Value = loadManQty.ToString();
                        MotherForm.dataGridView1.Rows[i].Cells["装卸人员"].Value = loadGroup.ToString();
                    }
                }
            }
            else
            {
                string loadGroup = sb.ToString();
                for (int i = 0; i < MotherForm.dataGridView1.Rows.Count; i++)
                {
                    string ID = MotherForm.dataGridView1.Rows[i].Cells["ID"].Value.ToString();
                    if (ID == transportID)
                    {
                        MotherForm.dataGridView1.Rows[i].Cells["装卸人数"].Value = loadManQty.ToString();
                        MotherForm.dataGridView1.Rows[i].Cells["装卸人员"].Value = loadGroup.ToString();
                    }
                }
            }
            MessageBox.Show("保存成功!");
           
            dataGridView1.DataSource = ProcSqlHelper.ExecuteDatatable(@"select aa.ID,aa.name,aa.idCard,aa.headman,aa.bz,isnull(bb.chk,0) chk from (select ID,name,idCard,headman,bz from FyloadMan where leave = 0) aa left join (select a.loadmanID,a.name,b.idCard,b.headman,b.bz,a.chk from FytransportLoadman a,FyloadMan b where a.loadmanID = b.ID and a.name = b.name and transportID =@transportID) bb on aa.ID = bb.loadmanID and aa.name = bb.name order by bz,headman desc,ID", CommandType.Text, new SqlParameter[] { new SqlParameter("@transportID", txtID.Text.Trim()) });
        }

阅读全文
0 0