日期:2014-05-17  浏览次数:20463 次

GridView中关联表的删除??急求!!!
我建了两张表,但是没有建关联然后在vs中配置数据源时,用的自定义Sql语句或存储过程,然后加了这两张表,把相同的字段做了个关联,现在想删除,不会弄。

我给GridView加了个HyperLinkFile列,text为删除,想点击删除,从而删除两张表的内容!!


求解求解求解求解

------解决方案--------------------
delete from tb where id=传进来要删除的ID
------解决方案--------------------
写 两个删除语句、 条件为这两张表关联时的条件:比如都是用ID做关联
delete from tb1 where id=传进来要删除的ID 
delete from tb2 where id=传进来要删除的ID 

------解决方案--------------------
假如A表为B表的子表,正确的逻辑是先删除A中和B表要删除的ID有关系的数据,然后再删除A表中ID为传进来ID的数据。
------解决方案--------------------
探讨

能代码吗?

------解决方案--------------------
<%@ Page Language="C#" Debug="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("Id", typeof(System.Int32)));
dt.Columns.Add(new System.Data.DataColumn("UserName", typeof(System.String)));
for (int i = 0; i < 8; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "【孟子E章】" + i.ToString();
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:HyperLinkField HeaderText="delete" Text="删除" DataNavigateUrlFields="Id" DataNavigateUrlFormatString="~/Delete.aspx?id={0}" />
</Columns>
</asp:GridView>
</form>
</body>
</html>


Delete.aspx里面
Page_Load
String id=Request.QueryString["id"];
String sql;
sql = "delete from tb1 where ParentId=" + id; 
SqlCommand command = new SqlCommand(sql , connection);
command.ExecuteNonQuery();

sql = "delete from tb3 where Id=" + id; 
command = new SqlCommand(sql , connection);
command.ExecuteNonQuery();