类型数据表typeTB结构:
id    int    4
info  vc     20文章数据表articleTB结构:
id      int    4
tid     int    4
topic   vc     50
pic     vc     50数据表typeTB记录:
 id    info
1     足球
2     娱乐
3     科技数据表articleTB记录:
id    tid      topic       pic
1     2        2007影帝    1.gif,2.jpg
2     2        2007歌王    34.jpg
3     1        他中足采现有以上数据,当我在删除数据表typeTB中的id字段值等于2时的记录时,相应要删除数据表articleTB中tid字段值等于2的所有记录,包括记录相对应的图片文件。
如果在asp中,我可这样循还地做删除操作:
Set rs = Server.CreateObject("ADODB.Recordset")
sql = "Select * From typeTB where id=2"
rs.open sql,conn,3,2
if not rs.bof and not rs.eof then
    Set rs1 = Server.CreateObject("ADODB.Recordset")
    sql1 = "Select * From articleTB where tid="&rs("id")&""
    rs1.open sql1,conn,3,2
    tcount=rs1.RecordCount
    if not rs1.bof and not rs1.eof then
        for i=1 to tcount
            id=rs1("id")
            pic=rs1("pic")
            //删除图片文件操作
            conn.Execute "Delete From articleTB Where id="&id&" "
            rs.movenext
            if rs.eof then exit for
next
    end if
    rs1.close
end if
rs.close
conn.Execute "Delete From typeTB where id=2 "请问在C#中,是如何删除的呢,非常希望得到您的指教!!!有分时一定给!!!!!
    

解决方案 »

  1.   

    用ado.net 中的command类,我用的sqlserver数据库,把连接字符串换成你的就行,不知道符合要求不。SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source= ;Initial Catalog= ;User ID= ;Password= ";
    conn.Open();
    string sql="delete from typeTB where id=2";
    SqlCommand com = new SqlCommand(sql,conn);
    com.ExecuteNonQuery();
    conn.Close();
      

  2.   

    数据表articleTB中有相对应的记录没有被删除呀!
      

  3.   

    # lye2000000_super说的:“建议你用存储过程,这样可以全部删掉,并且可以把要删除文件名全部返回回来,然后把文件删除掉。”是个办法。谢谢!
      

  4.   

    不好意思啊,没仔细看题目,我用笨方法来做吧,不过肯定是对的: SqlConnection conn = new SqlConnection();
     conn.ConnectionString = "Data Source=;Initial Catalog=;User ID=;Password=";
     conn.Open();
     string sql="select * from typeTB where id=2";
     DataSet ds = new DataSet();
     SqlDataAdapter da = new SqlDataAdapter();
     SqlCommand cmd = new SqlCommand(sql,conn);
     da.SelectCommand = cmd;
     da.Fill(ds,"typeTB");
     for (int i = 0; i < ds.Tables["typeTB"].Rows.Count; i++)
     {
        string sql1 = "delete from articleTB where tid="+ds.Tables["typeTB"].Rows[i]["id"]; 
         SqlCommand cmd1 = new SqlCommand(sql1,conn);
         cmd1.ExecuteNonQuery();
     }
     SqlCommand cmd2 = new SqlCommand("delete from typeTB where id=2", conn);
     cmd2.ExecuteNonQuery();
     conn.Close();
      

  5.   

    麻烦lye2000000_super朋友帮我写一下这个存储过程好不?我写不来,再说有了这个存储过程实例,以后我用来修改一下也可用于别的了。谢谢!
      

  6.   

    可用存储过程:
    create or replace deleteRecord (count in int) is
    begin
      delete from typeTB where id=count;
      delete from articleTB where id=count;
    end deleteRecord;
      

  7.   

    shuanglinghappy朋友好!这个好象没有返回值呀。
    因我对存储过程不懂呀,麻烦您帮我写详细些好没?谢谢!!!
      

  8.   

    注意sql语句
    SqlConnection   conn   =   new   SqlConnection(); 
    conn.ConnectionString   =   "Data   Source=   ;Initial   Catalog=   ;User   ID=   ;Password=   "; 
    conn.Open(); 
    string   sql="delete   from   typeTB   where   id=2 delete from articleTB   where tid =2"; 
    SqlCommand   com   =   new   SqlCommand(sql,conn); 
    com.ExecuteNonQuery(); 
    conn.Close(); 
      

  9.   

    create proc aa(@id int=2,@pics varchar(8000) output)
    as
    set @pics=''
    select @pics=@pics+','+pic from articleTB
    where tid=@id
    set @pics=stuff(@pics,1,1,'')以下是执行语句:
    declare @pics varchar(8000)
    EXEC aa 2,@pics output
    select @pics 谢谢所以帮忙的朋友!!!