protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = GridView2.Rows[e.RowIndex];
//int id = Int32.Parse(row.Cells[0].Text);
string aid = (row.Cells[1].Text);
var reh = from rh in db.Researches //ลบข้อมูลตารางงาน
where rh.Researcher.ID_Researcher == int.Parse(aid)
select rh;
foreach (var rh in reh)
{
db.Researches.DeleteOnSubmit(rh);
}
var acc = from a in db.Asstions //ลบข้อมูลตารางผู้ร่วมงาน
where a.Researcher.ID_Researcher == int.Parse(aid)
select a;
foreach (var a in acc)
{
db.Asstions.DeleteOnSubmit(a);
}
var ree = (from r in db.Researchers //ลบข้อมูลนักวิจัย
where r.ID_Researcher == int.Parse(aid)
select r).FirstOrDefault();
db.Researchers.DeleteOnSubmit(ree);
db.SubmitChanges();
}
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = GridView2.Rows[e.RowIndex];
//int id = Int32.Parse(row.Cells[0].Text);
string aid = (row.Cells[1].Text);
var reh = from rh in db.Researches //ลบข้อมูลตารางงาน
where rh.Researcher.ID_Researcher == int.Parse(aid)
select rh;
foreach (var rh in reh)
{
db.Researches.DeleteOnSubmit(rh);
}
db.Researchers.DeleteOnSubmit(reh);
db.SubmitChanges();
var acc = from a in db.Asstions //ลบข้อมูลตารางผู้ร่วมงาน
where a.Researcher.ID_Researcher == int.Parse(aid)
select a;
foreach (var a in acc)
{
db.Asstions.DeleteOnSubmit(a);
}
db.Researchers.DeleteOnSubmit(acc);
db.SubmitChanges();
var ree = (from r in db.Researchers //ลบข้อมูลนักวิจัย
where r.ID_Researcher == int.Parse(aid)
select r).FirstOrDefault();
db.Researchers.DeleteOnSubmit(ree);
db.SubmitChanges();
}
ทำได้แล้วครับ นั่งทำหลายชั่วโมง ต้องเรียงลำดับตารางให้ถูก ลบตารางลูกที่สัมพันธ์ให้หมดก่อน
แล้งค่อยลบตารางแม่ อ่านภาษาอังกฤษมา
I'm looking for ideas on deleting data from the multiple SQL tables with LINQ (C#).
My existing solution does the following:
1. DeleteAllOnSubmit( Subset of the data from the child table)
2. DeleteAllOnSubmit( Subset of the data from another child table)
3. DeleteAllOnSubmit( Data from the parent table)
4. CommitChanges()
All this resides within a single method, which makes it ugly and not re-usable.
Code (C#)
var acc = from a in db.Asstions
where a.Researcher.ID_Researcher == int.Parse(aid)
select a;
foreach (var a in acc)
{
db.Asstions.DeleteOnSubmit(a);
}
var reh = from rh in db.Researches
where rh.Researcher.ID_Researcher == int.Parse(aid)
select rh;
foreach (var rh in reh)
{
var acc2 = from a2 in db.Asstions //ลบผู้ร่วมของตารางงาน
where a2.Research.ID_Research == rh.ID_Research
select a2;
foreach (var a2 in acc2)
{
db.Asstions.DeleteOnSubmit(a2); //ลบผู้ร่วม
}
db.Researches.DeleteOnSubmit(rh); //ลบงาน
}
var ree = (from r in db.Researchers
where r.ID_Researcher == int.Parse(aid)
select r).FirstOrDefault();
db.Researchers.DeleteOnSubmit(ree);
db.SubmitChanges();