LINQ to SQL array list insertion

how insert data array to database with linq to sql? 

 DataClassesDataContext db = new DataClassesDataContext();
    simpleTbl tbl = new simpleTbl();
    string[] str =File.ReadAllLines(Server.MapPath("~/str.txt"));
 
    for (int i = 0; i <= 10; i++)
    {
       tbl.Name =str[i];
    }
  db.simpleTbl.InsertOnSubmit(tbl);
    db.SubmitChanges();

but dosen't work

How to do this?

Last updated:11/20/2014 11:48:48 PM

1 Answers

Sumit Kesarwani
Sumit Kesarwani

using(DataClassesDataContext db = new DataClassesDataContext())
{
    string[] strings =File.ReadAllLines(Server.MapPath("~/str.txt"));
    foreach (var str in strings )
    {
        db.simpleTbl.InsertOnSubmit(new simpleTbl(){ Name = str });
    }
    db.SubmitChanges();
}

You need to insert a new item for each entry in the string array

Answer