SqlDataReader.Getordinal方法缺少告诉用户给定列名不存在的功能 [英] SqlDataReader.Getordinal Method missing capability to tell user that given column name doesn't exist

查看:298
本文介绍了SqlDataReader.Getordinal方法缺少告诉用户给定列名不存在的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

您不认为SqlDataReader.Getordinal方法应以某种方式实现,以便如果给定的列不存在,则应以-1返回序数.

Do you not think SqlDataReader.Getordinal method should be implemented in a way so that if given column does not exist in it, it should return ordinal as -1

在遇到可选列的情况下,我遇到了这种情况,我不得不遍历循环以获取索引.

I faced the situation in case of an optional column and I had to go through loops to get the index.

这只是我想分享的观点.

It is just my view which I wanted to share.

谢谢

Vikas Agarwal

Vikas Agarwal

推荐答案

vikas,

>>>"您不认为SqlDataReader.Getordinal方法应以某种方式实现,以便如果给定的列中不存在该列,则应将序数返回为-1"

>>"Do you not think SqlDataReader.Getordinal method should be implemented in a way so that if given column does not exist in it, it should return ordinal as -1"

这是一个好主意,我们可以自己在自定义方法中实现类似的功能.

It is a good idea, and we can implement similar function in a custom method by ourselves.

public static int GetOrdinal(SqlDataReader reader, string name)
{
    List<string> fieldNames = new List<string>();
    for (int i = 0; i < reader.FieldCount; i++)
    {
        fieldNames.Add(reader.GetName(i));
    }
    return fieldNames.IndexOf(name);
}

while (reader.Read())
{
    int index = GetOrdinal(reader, "columnName");
}

最好的问候,
王丽

Best Regards,
Li Wang


这篇关于SqlDataReader.Getordinal方法缺少告诉用户给定列名不存在的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆