0

I know how to insert data into a database one by one but I want to insert multiple pieces of data into database and I don't know how to do this.

Here is my code for a "one-time" insert of data:

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

bloggerDB.bloggers.InsertOnSubmit(BlogerToaddData);
bloggerDB.SubmitChanges();

but how to insert multiple rows of data?

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

This is not working

1 Answer 1

2

Well you need to create multiple objects. You can either add them one at a time to the list of pending changes:

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

Or use 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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.