我有一个列表集合:
List<Person> people = new List<Person>();
Person p1 = new Person();
p1.FirstName = "Nikos";
p1.LastName = "Kantzelis";
p1.Email = "nikolaosk@hotmail.com";
p1.Height = 1.78m;
p1.Weight = 88.5m;
Person p2 = new Person();
p2.FirstName = "James";
p2.LastName = "Rowling";
p2.Email = "jamesr@hotmail.com";
p2.Height = 1.98m;
p2.Weight = 98.25m;
Person p3 = new Person();
p3.FirstName = "George";
p3.LastName = "Graham";
p3.Email = "graham@yahoo.co.uk";
p3.Height = 1.88m;
p3.Weight = 81.5m;
people.Add(p1);
people.Add(p2);
people.Add(p3);
现在我想使用存储过程将此List Collection中的值保存到DB中的某些表中。
请告知。
此致 爬完
答案 0 :(得分:2)
我不确定你是否在寻找更高档的东西,但你可以这样做:
foreach (Person person in people)
{
//example code
//save each person in the list to the database
SavePerson(person.Name, person.Age, ...);
}
如果您愿意,也可以使用LINQ这样做:
//example code
//save each person in the list to the database
people.ForEach(person => SavePerson(person.Name, person.Age, ...));
答案 1 :(得分:0)
如果您的数据库是SQL 2005(或者是2008)及以上,您可以将对象列表序列化为XML,然后将整个XML blob传递给参数中的SP,然后使用MSSQL的XML查询功能来解析XML并将数据插入表中。
排序INSERT INTO Person SELECT * FROM XML
这将允许单个SP调用,它可以包装在SP内的事务中(而不是跨越进程边界)。