Commandbehavior.sequentialaccess异常。尝试从列序号'0'读取无效。 [英] Commandbehavior.sequentialaccess exception. Attempt to read from column ordinal '0' is not valid.

查看:189
本文介绍了Commandbehavior.sequentialaccess异常。尝试从列序号'0'读取无效。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

EntityDataReader

来填充

List<SelectListItem> products

只有一列类型为string:[prod_name_pharma_form]

我收到此异常:

from MS SQL table that has only one column of type "string": [prod_name_pharma_form]
I am getting this exception:

Attempt to read from column ordinal '0' is not valid.  With CommandBehavior.SequentialAccess, you may only read from column ordinal '1' or greater.'





我尝试过:



我使用的代码与多列的表格完美配合:





What I have tried:

I am using this code that works perfectly with tables of more than one column:

<pre>public JsonResult AjaxProducts()
        {
            List<SelectListItem> products = new List<SelectListItem>();
            string query = "SELECT p.prod_name_pharma_form FROM Entities.FDF_FAMILY_PROD_NAME as p";
            using (EntityConnection con = new EntityConnection("name=Entities"))
            {
                using (EntityCommand cmd = new EntityCommand(query))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    con.Open();
                    using (EntityDataReader sdr = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
                    {
                        while (sdr.Read())
                        {
                            products.Add(new SelectListItem
                            {
                                Value = sdr["prod_name_pharma_form"].ToString(),
                                Text = sdr["prod_name_pharma_form"].ToString()
                            });
                        }
                    }
                }
            }

            return Json(products);
        }



我理解使用SequentialAccess我必须以正确的顺序读取(选择)列以避免这种异常,但是我该怎么做如果有一个列的表?


I understand that with "SequentialAccess" I have to read (select) columns in the correct order to avoid this kind of exceptions, but What do I do in case having a table of one column?

推荐答案

可能是因为你正在阅读它两次。试试这个

It could be because you're reading it twice. Try this
while (sdr.Read())
{
    string name = sdr["prod_name_pharma_form"].ToString();

    products.Add(new SelectListItem
    {
        Value = name,
        Text = name
    });
}


在代码周围放置一个 try / catch 块,以便正确调试它。



BTW,如果你没有加入,你不需要别名p。



在尝试访问 sdr sdr.HasRows 为真> object,以及在尝试检索所需列的值之前检查以确保列模式中存在所需的列。简而言之,假设您的数据集包含您期望的内容绝对不安全。
Put a try/catch block around your code so you can properly debug it.

BTW, you don't need the alias "p" if you're not doing a join.

I would also add a check to make sure sdr.HasRows is true before trying to access the sdr object, as well as check to make sure the desired column(s) exist within the column schema before trying to retrieve the value of the desired column(s). In short, it is NEVER safe to assume that you're dataset will contain what you're expecting.


这篇关于Commandbehavior.sequentialaccess异常。尝试从列序号'0'读取无效。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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