I have a model, and a list of instances (about 5000) I need copied into a database.
I'm trying to assimilate my objects into a datatable but I don't know how to do it:
public class BookingType {
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int RandomProperty { get; set; }
public int RandomProperty2 { get; set; }
}
public void InsertSomeStuff(IEnumerable<BookingType> bookings) {
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)) {
conn.Open();
DataTable dt = new DataTable();
using (SqlBulkCopy copy = new SqlBulkCopy(conn)) {
copy.ColumnMappings.Add(0, 1);
copy.DestinationTableName = "dbo.Bookings";
copy.WriteToServer(dt);
}
}
}
How do I do this?