StringBuilder字符串拼接用法舉例
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()) });
}
{
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()) });
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.