向数据库wp8插入多行数据

本文关键字:数据 插入 数据库 wp8 | 更新日期: 2023-09-27 17:54:38

我知道如何一个接一个地插入数据到数据库中,但我想插入多个数据块到数据库中,我不知道如何做到这一点。

下面是我的"一次性"插入数据的代码:

Bloggers BlogerToaddData = new Blogger
{
     Interest = txtIntrest.Text.ToString(),
     Name = txtname.Text.ToString(),
     Totalposts = Convert.ToInt32(txtPost.Text)
};
bloggerDB.bloggers.InsertOnSubmit(BlogerToaddData);
bloggerDB.SubmitChanges();

但如何插入多行数据?

Bloggers BlogerToaddData = new Bloggers
{Interest = "wy",Name = "opwm",Totalposts =1},
{Interest = "wy",Name = "opwm",Totalposts =1},
{Interest = "wy",Name = "opwm",Totalposts =1},

这个不能用

向数据库wp8插入多行数据

您需要创建多个对象。您可以将它们逐个添加到挂起更改列表中:

var bloggs = bloggerDB.bloggers;
bloggers.InsertOnSubmit(new Bloggers { Interest = "wy", Name = "opwn", TotalPosts = 1 });
bloggers.InsertOnSubmit(new Bloggers { Interest = "ab", Name = "abcd", TotalPosts = 2 });
bloggers.InsertOnSubmit(new Bloggers { Interest = "cd", Name = "1234", TotalPosts = 3 });
bloggerDB.SubmitChanges();

或者使用InsertAllOnSubmit:

bloggerDB.bloggers.InsertAllOnSubmit(new[] {
    new Bloggers { Interest = "wy", Name = "opwn", TotalPosts = 1 },
    new Bloggers { Interest = "ab", Name = "abcd", TotalPosts = 2 },
    new Bloggers { Interest = "cd", Name = "1234", TotalPosts = 3 }
});
bloggerDB.SubmitChanges();