ASP.net修改多个关联的表中的某个字段的值的SQL语句是怎么写?

[复制链接]
查看11 | 回复2 | 2011-5-19 23:43:29 | 显示全部楼层 |阅读模式
department表:depid、department、depinfo;主键是depid
assessment表:assid、staname、department;主键是assid。这两个表的department字段值是关联的。我想要修改主表department的department的值时,怎么实现同步修改assessment的该字段值?假如用触发器又怎么写?
下面是一段代码:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{//更新

string id =GridView1.DataKeys[e.RowIndex].Value.ToString();//获取主键值

string depid = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;//部门编号

string depname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;//部门名称

string num =((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;//部门人数

string info=((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;//获取部门简介

string sqlUpdate = "update department set depid='" + depid + "',department='" + depname + "',depcount='" + num + "',depinfo='" + info + "' where depid='" + id + "'";

myc.sqlUpdate(sqlUpdate);//执行修改

GridView1.EditIndex = -1;

getDepartment();

}

回复

使用道具 举报

千问 | 2011-5-19 23:43:29 | 显示全部楼层
要先修改assessment表中的值,再修改department表中的值,try下,出错回滚事务,至于触发器是要在数据库中先建好的sqlserver中有级联删除和级联修改
回复

使用道具 举报

千问 | 2011-5-19 23:43:29 | 显示全部楼层
写个存储过程(用事务同步执行),或者再执行一次SQL
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行