我正在尝试将列表转换为数据表。我当前的代码如下所示,但是 GetProperties(BindingFlags.Public | BindingFlags.Instance) 不返回任何属性。
订购对象:
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;
}
1) List<Order> OrdersToProcess = new List<Order>();
2) BulkInsert(OrdersToProcess, "TestTableName");
3) private void BulkInsert(List<Order> 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<T>(IList<T> 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;
}
任何建议都会非常有帮助。
我正在尝试通过从列表的属性中提取列和值来将列表转换为数据表。
在 Order 类中只有字段。我建议使用
.GetFields()
方法
public DataTable ToDataTable<T>(IList<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties. Fixed here ------------
PropertyInfo[] Props = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance);
//Gain fields instead of properties -------------
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;
}
至于
ToDataTable
方法中的 null 异常。我不确定,但尝试将 <Order>
类型放入代码中的通用类型括号中
private void BulkInsert(List<Order> orders, string tablename)
{
//Put here -------------------------------------------------------------------
DataTable pDt = ToDataTable<Order>(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();
}
}
在我的代码中,我尝试为整个方法赋予任何价值。每次它都没有变得空