[英]Ado.net disconnected update whole table
I know there are much more articles about this question, but my question is a special case -as I think- 我知道关于这个问题的文章更多,但是我的问题是一种特殊情况-正如我认为的那样-
I have a DataSet filled with Datatable named 'Categories' by OleDataAdapter. 我有一个由OleDataAdapter命名为“ Categories”的数据表填充的数据集。 After filling 'Categories' table from the database on a local PC, I've made a huge editing to it (adding rows -maybe thousands- and updating fields).
从本地PC上的数据库填充“类别”表后,我对其进行了巨大的编辑(添加行-可能是数千行-和更新字段)。
now, I want to update all of these edits to the database table on the PC. 现在,我想将所有这些编辑更新到PC上的数据库表。 how to do this as fast as possible?
如何尽快做到这一点?
Try SqlBulkCopy 试试SqlBulkCopy
https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlbulkcopy?redirectedfrom=MSDN&view=netframework-4.7.2 https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlbulkcopy?redirectedfrom=MSDN&view=netframework-4.7.2
a quick sample: 快速样本:
DataTable dt = new DataTable();
foreach (var itm in sampleList) {
DataRow row = dt.NewRow();
row["Field1"] = itm.Field1;
row["Field2"] = itm.Field2;
row["Field3"] = itm.Field3;
dt.Rows.Add(row);
}
using (SqlConnection cn = new SqlConnection(connectionString)) {
cn.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(cn)) {
bulkCopy.DestinationTableName = "dbo.Categories";
bulkCopy.WriteToServer(dt);
}
cn.Close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.