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