日期:2014-05-18  浏览次数:20889 次

[小白问题]如何在删除父类的同时删除子类
数据结构:


数据层删除类 
dal.Delete 
public void Delete(int cate_id) 


StringBuilder strSql=new StringBuilder(); 

strSql.Append("delete from Gcategory "); 
strSql.Append(" where cate_id=@cate_id "); 
SqlParameter[] parameters = { 
new SqlParameter("@cate_id", SqlDbType.Int,4)}; 
parameters[0].Value = cate_id; 

DbHelperSQL.ExecuteSql(strSql.ToString(),parameters); 

业务逻辑层 bllGcategory.Delete 
public void Delete(int cate_id) 


dal.Delete(cate_id); 


表示层 删除啊按钮触发事件 
if (e.CommandName == "Delete") 

int iUser_id = Convert.ToInt32((e.Item.FindControl("txtCate_id") as TextBox).Text.Trim()); 
bllGcategory.Delete(iUser_id); 
BindData(); 


现在的处理结果是,父类删除后,子类仍然会保留在数据库中。 

如何能在父类删除的同时,相应的子类也删除呢?

------解决方案--------------------
既然你写sql语句,不会连inner join都不知道滴。
------解决方案--------------------
那要看你数据库是怎么设计的了吧
------解决方案--------------------

/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">多条SQL语句</param>
public static int ExecuteSqlTran(List<String> SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
SqlTransaction 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;
}
}
}
------解决方案--------------------
探讨
数据库就是那样啊--parent_id是父类 cate_id是子类 无父类时parent_id为0

引用:

那要看你数据库是怎么设计的了吧

------解决方案--------------------
你可以移到sql server板块去学学sql语句怎么写。一个删除(你所说的)“子类”记录的语句类似于:
SQL code
delete a from [子类] a inner join [父类] b on a.xxxx=b.yyy where b.查询条件