ADO.Net:检查字段名称存在于IDataRecord [英] ADO.Net: Check if field name exists on IDataRecord
问题描述
是否有只有在FIELD_NAME存在于IDataRecord,目前我使用的尝试{...}赶上{...} 从IDataRecord获得FIELD_NAME价值更好的办法块,但是这是某种的在接下来的错误恢复的。一些替代品?
///<总结>
///返回从IDataRecord只有FIELD_NAME存在的列值。
///< /总结>
公共静态Tresult ValueIfExists< Tresult>(这IDataRecord记录,串FIELD_NAME)
{
尝试{返回record.Value< Tresult>(record.GetOrdinal(FIELD_NAME)); }
抓{返回默认值(Tresult); }
}
///<总结>
///通过索引返回从IDataRecord accecing列的值。
///< /总结>
公共静态Tresult值小于; Tresult>(这IDataRecord记录,诠释field_index)
{
返回record.IsDBNull(field_index)?默认(Tresult):
(Tresult)Convert.ChangeType(记录[field_index]的typeof(Tresult));
}
我已经改变了我的 ValueIfExists 的功能,以反映你的想法,所以它看起来是这样的:
公共静态Tresult ValueIfExists2< Tresult>(这IDataRecord记录,串FIELD_NAME)
{
对于(INT指数= 0;指数< record.FieldCount;指数++)
{
如果(record.GetName(指数).Equals(FIELD_NAME,StringComparison.InvariantCulture))
{
返回record.Value&其中; Tresult>(record.GetOrdinal(FIELD_NAME));
}
}
返回默认(Tresult);
}
您说的没错,异常不应被用于正常的程序流程。
在 GetOrdinal
方法适用于,你知道你会得到什么字段,如果一个字段缺少这是应该引起异常的错误的情况。
如果您不知道哪些字段的结果得到的,你应该避免 GetOrdinal
方法。而是可以得到所有的名字和他们的索引字典,可以作为替代品使用 GetOrdinal
方法:
公共静态字典<字符串,INT> GetAllNames(此IDataRecord记录){
VAR的结果=新字典<字符串,INT>();
的for(int i = 0; I< record.FieldCount;我++){
result.Add(record.GetName(ⅰ),ⅰ);
}
返回结果;
}
您可以使用的containsKey
的方法来检查名称存在于词典,或 TryGetValue
方法检查名称存在,并得到它的指数它在一个单一的操作。
在 GetOrdinal
方法首先做了情况sensetive搜索的名称,如果失败了,做了情况insensetive搜索。这是不提供字典,因此,如果你想要的确切行为你宁愿存储名字的数组,并通过他们,当你想找到索引写一个方法来循环。
Is there a better way for getting the field_name value from a IDataRecord only if the field_name exists in the IDataRecord, currently I'm using a try{...} catch{...} block, but this is some kind of On Error Resume next. Some alternatives?
/// <summary>
/// Returns column value from IDataRecord only if field_name exists.
/// </summary>
public static Tresult ValueIfExists<Tresult>(this IDataRecord record, string field_name)
{
try { return record.Value<Tresult>(record.GetOrdinal(field_name)); }
catch { return default(Tresult); }
}
/// <summary>
/// Returns column value from IDataRecord accecing by index.
/// </summary>
public static Tresult Value<Tresult>(this IDataRecord record, int field_index)
{
return record.IsDBNull(field_index) ? default(Tresult) :
(Tresult)Convert.ChangeType(record[field_index], typeof(Tresult));
}
I have changed my ValueIfExists function to reflect your ideas, so it looks like this:
public static Tresult ValueIfExists2<Tresult>(this IDataRecord record, string field_name)
{
for (int index = 0; index < record.FieldCount; index++)
{
if (record.GetName(index).Equals(field_name, StringComparison.InvariantCulture))
{
return record.Value<Tresult>(record.GetOrdinal(field_name));
}
}
return default(Tresult);
}
You are right that exceptions should not be used for normal program flow.
The GetOrdinal
method is intended for situations where you know what fields you get, and if a field is missing that is an error that should result in an exception.
If you don't know which fields you get in the result, you should avoid the GetOrdinal
method. You can instead get all the names and their index into a dictionary that you can use as replacement for the GetOrdinal
method:
public static Dictionary<string, int> GetAllNames(this IDataRecord record) {
var result = new Dictionary<string, int>();
for (int i = 0; i < record.FieldCount; i++) {
result.Add(record.GetName(i), i);
}
return result;
}
You can use the ContainsKey
method to check if the name exists in the dictionary, or the TryGetValue
method to check if the name exists and get it's index it does in a single operation.
The GetOrdinal
method first does a case sensetive search for the name, and if that fails it does a case insensetive search. That is not provided by the dictionary, so if you want that exact behaviour you would rather store the names in an array and write a method to loop through them when you want to find the index.
这篇关于ADO.Net:检查字段名称存在于IDataRecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!