C# SqlBulkCopy 指定欄位-bulkCopy.ColumnMappings.Add("來源TABLE的欄位A", "目標TABLE的欄位A");

C# SqlBulkCopy 指定欄位-bulkCopy.ColumnMappings.Add("來源TABLE的欄位A", "目標TABLE的欄位A");

C# SqlBulkCopy 指定欄位-

using (SqlConnection connection = sqlConn)
            {
                connection.Open();
                SqlTransaction sqlTrans = connection.BeginTransaction();
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, sqlTrans))
                {
                    DataTable dt = dtt_visitors;
                    bulkCopy.DestinationTableName = "t_visitors";

                    //對應資料行
                    //bulkCopy.ColumnMappings.Add("DataTable的欄位A", "資料庫裡的資料表的的欄位A");

                    bulkCopy.ColumnMappings.Add("id", "id");

                    bulkCopy.BatchSize = 1000;
                    bulkCopy.BulkCopyTimeout = 60;

                    try
                    {
                        bulkCopy.WriteToServer(dt);
                        sqlTrans.Commit();

                        MessageBox.Show("完成");
                    }

                    catch (Exception)
                    {
                        sqlTrans.Rollback();                       
                    }

                   


                }

            }

 

自我LV~