0
A
回答
1
考虑您的列表:IList<Customer> foo;
使用LINQ to SQL
创建LINQ到SQL的DataContext,它是这样的:
using (var db = new CustomerDataContext())
{
db.Customer.InsertAllOnSubmit(foo);
db.SubmitChanges();
}
使用存储过程
using (var conn = new SqlConnection())
{
using (var cmd = new SqlCommand(conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "InsertCustomer";
foreach (Customer c in foo)
{
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@CustomerName", c.Name);
conn.Open(dbConnString);
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
这段代码可能不是复制/粘贴就绪,也许是某处的语法/名称/属性问题,但它绝对是使用ADO.NET的一个起点。
0
如果您正在使用,那么你可以如下做到基本的SqlConnection类:
IList<Record> records = new List<Record>();
SqlConnection connection = new SqlConnection("Give connection string here");
connection.Open();
var insertCommand = connection.CreateCommand();
foreach (var record in records)
{
insertCommand.CommandText = string.Format("Insert into table1 (field1, field2) values ({0}, {2})", record.field1,
record.field2);
insertCommand.ExecuteScalar();
}
connection.Close();
相关问题
- 1. 如何在Python代码中迭代并插入列表中
- 2. Excel表单打开并迭代写入
- 3. 在IList上调用.Last()是否迭代整个列表?
- 4. 什么接口代表的插入顺序LinkedHashSet迭代
- 5. 替代RemoveAt并插入列表项目
- 6. Logstash迭代内部对象并插入到数据库
- 7. 迭代集合对象并使用节点j插入到db
- 8. 基于插入顺序迭代散列?
- 9. STL向量,迭代器和插入(C++)
- 10. 转换树插入到迭代函数
- 11. Scala HashMap:迭代插入顺序?
- 12. ACCES:插入选择与迭代条件
- 13. 迭代数组并加入Bluebird
- 14. 链接列表使用插入降序迭代
- 15. 迭代通过链接列表插入STL向量值
- 16. 在迭代中插入列表时不安全吗?
- 17. Web表单插入并触发插入
- 18. 迭代每个表,并获得信息
- 19. 如何并行迭代两个列表?
- 20. 使用迭代器合并列表
- 21. 迭代嵌套列表并更改值
- 22. 排序的数据结构快速迭代,插入,通过迭代
- 23. 硒C#,IList <IWebElement>迭代通过和断言值
- 24. 如何以相反顺序迭代.Net IList集合?
- 25. 迭代NHibernate加入
- 26. 在嵌入式Elixir中迭代列表
- 27. 迭代插入二进制搜索tree.Debug C++代码
- 28. 为什么struts迭代会在源代码中插入空行?
- 29. 迭代python列表:迭代顺序
- 30. SQL多重插入一个表,而循环/迭代另一个表?
你能给喜欢你是如何连接到数据库的一些细节?你正在使用ADO或Linq或其他东西? – JPReddy 2010-09-21 02:32:48
为什么你需要循环? – 2010-09-21 02:40:41
我正在使用ado.net,企业库IReader – SRA 2010-09-21 02:41:52