Order Object:
Code: Select all
class Order
{
public string SSCC;
public string Code;
public string ToSiteMaterial;
public string ToSiteProductCode;
public string FromSiteMaterial;
public string FromSiteProductCode;
public string Weight;
public string Prday;
public string Expire1;
public string Expire2;
public string Expire3;
public string slday;
public string UnitExternalBatch;
public string UnitLotOverride;
public string PalletSSCC;
public string PalletID;
public DateTime CreatedDateTime;
public string processed;
public string verified;
public string PackErrorMessage;
public string PalletErrorMessage;
public string LinkErrorMessage;
public string NewPackSSCC;
public string Tare;
public string Manufacturer;
public string IntakeLabelLayout;
public string Pieces;
public string TimesProcessed;
public string FixedCode;
}
Code: Select all
1) List OrdersToProcess = new List();
2) BulkInsert(OrdersToProcess, "TestTableName");
Code: Select all
3) private void BulkInsert(List orders, string tablename)
{
DataTable pDt = ToDataTable(orders);
//new IsoDateTimeConverter { DateTimeFormat = "dd/MM/yyyy" });
var constr = ConnectionStringGoesHere
using (SqlConnection connection = new SqlConnection(constr))
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
{
connection.Open();
bulkCopy.DestinationTableName = tablename;
// How many records to send to the database in one go (all of them)
bulkCopy.BatchSize = 100000;
bulkCopy.BulkCopyTimeout = 999999999;
// Load the data to the database
bulkCopy.WriteToServer(pDt);
// Close up
bulkCopy.Close();
connection.Close();
}
}
4) public DataTable ToDataTable(IList items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Setting column names as Property names
dataTable.Columns.Add(prop.Name);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}


Alle Vorschläge wären sehr hilfreich.
Das bin ich Versuchen Sie, eine Liste in eine Datentabelle umzuwandeln, indem Sie die Spalten und Werte aus den Eigenschaften der Liste extrahieren.