public long BulkCopy(string pNomeTabela, string pSqlCommand, BulkConfig pBulkConfig) { _rowsCopied = 0; try { using (var reader = (SqlDataReader)ConnectSource.ExecuteReader(pSqlCommand)) { if (reader.HasRows) { using (var bulkCopy = new SqlBulkCopy(ConnectTarget.Connection, pBulkConfig.Options, null)) { bulkCopy.SqlRowsCopied += BulkCopy_SqlRowsCopied; bulkCopy.NotifyAfter = 1; bulkCopy.BulkCopyTimeout = pBulkConfig.CopyTimeout; bulkCopy.BatchSize = pBulkConfig.BatchSize; bulkCopy.DestinationTableName = pNomeTabela; bulkCopy.WriteToServer(reader); } } reader.Close(); } } catch (Exception) { throw; } return(_rowsCopied); }
public string GerarScriptInsert(string pNomeTabela) { IDataReader dr = null; try { dr = ConnectSource.ExecuteReader(string.Format("SELECT * FROM {0} WITH(NOLOCK)", pNomeTabela)); string result = ""; string values; string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Insert"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path = Path.Combine(path, pNomeTabela + ".sql"); if (File.Exists(path)) { File.Delete(path); } using (StreamWriter sw = new(path, false, Encoding.ASCII)) { while (dr.Read()) { values = ""; for (int i = 0; i < dr.FieldCount; i++) { if (!object.ReferenceEquals(dr[i], DBNull.Value)) { values += "," + GetFormatDataValue(dr[i].GetType(), dr[i].ToString()); } else { values += ", NULL"; } } if (result != "") { result += ", (" + values[1..] + ")" + Environment.NewLine;