我正在根據來自 Nswag 作業室的規范撰寫 API 客戶端。我能夠使用提供的 檢索該資料client.PlansAsync(apikey).GetAwaiter().GetResult(),但我正在努力將回傳的 ICollection 轉換為能夠批量插入 Oracle 資料庫表的內容。
我試圖創建一個DataTable但是在轉換為資料表的程序中,拋出了一個例外。我懷疑它與集合中的可為空型別有關。
我的猜測是我應該嘗試使用 Entity Framework 進行插入,但對于這個特定的客戶端來說,添加所有額外的 EF Core 內容似乎有點過頭了。
我覺得 Oracle 批量復制方法非常適合我的意圖,但我一直在解決上面列出的問題。
任何幫助將不勝感激。
TIA
編輯:這是有問題的代碼。
//first in the calling class
ICollection<Plans> plansFromApi = client_.PlansAsync(apiKey).GetAwaiter().GetResult();
ListToDatTable listToDt = new();
List<Plans> ps = plansFromApi.ToList();
DataTable dt = listToDt.ToDataTable<Plans>(ps);
//second the List to Datatable class
public class ListToDataTable
{
public DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new(typeof(T).Name);
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach(T item in items)
{
var values = new object[Props.Length];
for(int i = 0; i < Props.Length; i )
{
values[i] = Props[i].GetValue(item);
}
//line where the exception is thrown
//System.ArgumentException: 'Input array is longer than the number of columns in this table.'
dataTable.Rows.Add(values);
}
}
}
編輯 2:這是來自 Nswag 作業室的內容。這只是我需要檢索的 15 個資料集之一。這不是我目前正在測驗的一個,因為那個有 25 個屬性,所以為簡潔起見,我將包括較小的一個。最后,它們都是一樣的,因為它們都將以完全相同的方式進行處理,是的,我也用這個資料集進行了測驗,并收到了相同的例外。
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.5.2.0 (Newtonsoft.Json v12.0.0.0)")]
public partial class ContactGroupedManufacturer
{
[Newtonsoft.Json.JsonProperty("lastContacted", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.DateTimeOffset? LastContacted { get; set; }
[Newtonsoft.Json.JsonProperty("vendorContactId", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public int VendorContactId { get; set; }
[Newtonsoft.Json.JsonProperty("ManufacturerId", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public int ManufacturerId { get; set; }
[Newtonsoft.Json.JsonProperty("website", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Website { get; set; }
}
這里區域幾行資料:
| 最后聯系 | 供應商聯系人 ID | 制造商 ID | 網站 |
|---|---|---|---|
| 6575 | 1848年 | ||
| 6599 | 2693 | ||
| 6604 | 8878 | 06/08/2018 | |
| 6692 | 6879 | ||
| 6930 | 4040 | 一些網址 |
uj5u.com熱心網友回復:
您可能對 DataTable 中的列數有問題。試試這個代碼:
public class ListToDataTable
{
public DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable();
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
bool columnsAlreadyCreated=false;
foreach(T item in items)
{
if (columnsAlreadyCreated==false)
{
for(int i = 0; i < Props.Length; i )
{
dataTable.Columns.Add(Props[i].Name,Props[i].PropertyType);
}
columnsAlreadyCreated=true;
}
var values = new object[Props.Length];
for(int i = 0; i < Props.Length; i )
{
values[i] = Props[i].GetValue(item);
}
//line where the exception is thrown
//System.ArgumentException: 'Input array is longer than the number of columns in this table.'
dataTable.Rows.Add(values);
}
}
}
uj5u.com熱心網友回復:
您可以將 IEnumerable 包裝在 IDataReader 中以傳遞給 BulkCopy 方法。參見例如: ObjectDataReader
擁有 IDataReader 后,將其傳遞給OracleBulkCopy.WriteToServer(IDataReader)See OracleBulkCopy。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/349018.html
