当前,我正在GetOleDbSchemaTable的帮助下获取表和列。我也想得到给定的列是否必需。
//backend response
const response = {
data: {
results: {
222: {
items: ['id1', 'id3']
},
333: {
items: ['id2', 'id4', 'id999 (UNKNOWN)']
}
}
}
};
//currently saved in redux state
const stateItems = [
{
id: 'id1',
name: 'item ONE'
}, {
id: 'id2',
name: 'item TWO'
}, {
id: 'id3',
name: 'item THREE'
}, {
id: 'id4',
name: 'item FOUR'
}, {
id: 'id5',
name: 'item FIVE (UNUSED)'
}, {
id: 'id6',
name: 'item SIX (UNUSED)'
}
];
// Create map of state items (only once each time stateItems changes):
const stateItemMap = new Map(stateItems.map(item => [item.id, item]));
// Map results (each time you get results):
const result = {};
for (const [key, {items}] of Object.entries(response.data.results)) {
result[key] = {
items: items.map(id => stateItemMap.get(id))
};
}
//final result should be:
const expectedFinalResult = {
222: {items: [{id: 'id1', name: 'item ONE'}, {id: 'id3', name: 'item THREE'}]},
333: {items: [{id: 'id2', name: 'item TWO'}, {id: 'id4', name: 'item FOUR'}]}
};
//both should be equal:
console.log(JSON.stringify(result, null, 4));
这样我就可以得到给定列的列和数据类型。现在,我想获取列表或使用一种方法可以获取表中的必填字段。
答案 0 :(得分:2)
使用GetSchema方法并询问COLUMNS集合将返回一个DataTable,您可以在其中找到IS_NULLABLE属性
using(OleDbConnection con = new OleDbConnection(......)
{
con.Open();
string[] rest = new string[] { null, null, tableName, null };
var schema = con.GetSchema("COLUMNS", rest);
foreach (DataRow row in schema.Rows)
Console.WriteLine($"Column={row["COLUMN_NAME"]}, NULLABLE={row["IS_NULLABLE"]}");
}
顺便说一句,通过使用您的方法进行检查,GetOleDbSchemaTable返回了相同的数据表,因此,它只是存储在IS_NULLABLE列中的值