Created
April 12, 2018 14:35
-
-
Save michaeldeongreen/e1bd179071f478e7c39bb84164c47640 to your computer and use it in GitHub Desktop.
How to use SqlBulkCopy with Strongly Typed DataTables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
namespace ConsoleApplication1.SqlBulkCopy | |
{ | |
public static class ListExtensions | |
{ | |
public static T AddNew<T>(this IList<T> list, T newItem) | |
{ | |
list.Add(newItem); | |
return newItem; | |
} | |
public static T AddNew<T>(this ICollection<T> list, T newItem) | |
{ | |
list.Add(newItem); | |
return newItem; | |
} | |
public static DataTable AsDataTable<T>(this IList<T> items) | |
{ | |
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); | |
var table = new DataTable(); | |
foreach (PropertyDescriptor prop in properties) | |
{ | |
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); | |
} | |
int t = table.Columns.Count; | |
foreach (T item in items) | |
{ | |
DataRow row = table.NewRow(); | |
foreach (PropertyDescriptor prop in properties) | |
{ | |
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; | |
} | |
table.Rows.Add(row); | |
} | |
return table; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ConsoleApplication1.SqlBulkCopy | |
{ | |
public interface ISqlBulkCopyService | |
{ | |
void Save(SqlBulkCopyConfiguration configuration); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Data.SqlClient; | |
namespace ConsoleApplication1.SqlBulkCopy | |
{ | |
public class SqlBulkCopyService : ISqlBulkCopyService | |
{ | |
public void Save(SqlBulkCopyConfiguration configuration) | |
{ | |
using (var connection = new SqlConnection(configuration.ConnectionString)) | |
{ | |
connection.Open(); | |
SqlTransaction transaction = connection.BeginTransaction(); | |
using (var bulkCopy = new System.Data.SqlClient.SqlBulkCopy(connection, SqlBulkCopyOptions.Default, transaction)) | |
{ | |
bulkCopy.BatchSize = configuration.BatchSize; | |
bulkCopy.DestinationTableName = configuration.TableName; | |
try | |
{ | |
foreach (var column in configuration.DataTable.Columns) | |
{ | |
bulkCopy.ColumnMappings.Add(column.ToString(), column.ToString()); | |
} | |
bulkCopy.WriteToServer(configuration.DataTable); | |
transaction.Commit(); | |
} | |
catch (Exception e) | |
{ | |
transaction.Rollback(); | |
connection.Close(); | |
throw new Exception(e.Message); | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Data; | |
namespace ConsoleApplication1.SqlBulkCopy | |
{ | |
public class SqlBulkCopyConfiguration | |
{ | |
public string ConnectionString { get; set; } | |
public int BatchSize { get; set; } | |
public DataTable DataTable { get; set; } | |
public string TableName { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ConsoleApplication1.SqlBulkCopy | |
{ | |
public class LoanApplication | |
{ | |
public int ApplicationId { get; set; } | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public DateTime CreatedDate { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static async Task Main(string[] args) | |
{ | |
//Usage | |
IList<LoanApplication> loanApplications = new List<LoanApplication>() { new LoanApplication() { ApplicationId = 1, CreatedDate = DateTime.Now, FirstName = "Michael", LastName = "Green" }, | |
new LoanApplication() { ApplicationId = 2, CreatedDate = DateTime.Now, FirstName = "Amy", LastName = "Green" }}; | |
if (loanApplications != null && loanApplications.Count() > 0) | |
{ | |
SqlBulkCopyConfiguration config = new SqlBulkCopyConfiguration() | |
{ | |
BatchSize = 100, | |
ConnectionString = "DatabaseConnectionString", | |
DataTable = ListExtensions.AsDataTable<LoanApplication>(loanApplications), | |
TableName = "dbo.SomeTableToInsertRecords" | |
}; | |
new SqlBulkCopyService().Save(config); | |
} | |
Console.ReadLine(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment