简体   繁体   中英

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. 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).

now, I want to update all of these edits to the database table on the PC. how to do this as fast as possible?

Try SqlBulkCopy

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM