您的位置:首页 > 其它

开放源码的对象关系映射工具ORM.NET 删除数据 Deleting Records using ORM.NET

2011-09-16 15:09 429 查看

删除一笔记录 Object].Delete()

下面的代码,删除FirstName为Tim,LastName为Brown的学生

DataManager dm = new DataManager(Config.Dsn);
dm.QueryCriteria.Clear();
dm.QueryCriteria.And(JoinPath.Student.Columns.FirstName,”Tim”)
.And(JoinPath.Student.Columns.LastName,”Brown”);
Student s = dm.GetStudent(FetchPath.Student);
s.Delete(); // marks the returned DataRow to be deleted
dm.CommitAll();  // performs the necessary insert,update and delete operations


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

删除多行记录

DataManager dm = new DataManager(Config.Dsn);
dm.QueryCriteria.And(JoinPath.Student.Columns.FirstName,"Tim")
.And(JoinPath.Student.Columns.LastName,"Brown");
StudentCollection students = dm.GetStudentCollection();
// check to ensure that there are record(s) to delete
if (students != null)
{
foreach (Student s in students)
s.Delete(); // loop through and mark for deletion
dm.CommitAll();  // Delete all datarows marked for deletion transitionally
}


 


先取出数据到Collection中,再用object.Delete标记为删除,在CommitAll方法中执行删除记录

 


删除主从表记录 Delete Parent and Child record(s)

请看代码,先读取Brown学生的数据和它联系方式,之后再删除联系方式和Brown学生记录。

dm.QueryCriteria.Clear();
dm.QueryCriteria.And(JoinPath.Student.Columns.LastName,"Brown");
// Get[Object] will retrieve Student and related Contact records
Student student = dm.GetStudent(FetchPath.Student.Contact);
student.Contact.Delete();      // mark the Parent Contact record to be deleted
student.Delete();              // mark Student record Root object to be deleted
dm.CommitAll();


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐