将多行数据插入数据库wp8

时间:2014-03-29 08:29:40

标签: c# windows-phone-8

我知道如何逐个将数据插入数据库,但我想将多个数据插入数据库,我不知道如何做到这一点。

这是我的“一次性”数据插入代码:

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},

这不起作用

1 个答案:

答案 0 :(得分:2)

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

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();