本文介绍了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?
推荐答案
while (sdr.Read())
{
string name = sdr["prod_name_pharma_form"].ToString();
products.Add(new SelectListItem
{
Value = name,
Text = name
});
}
这篇关于Commandbehavior.sequentialaccess异常。尝试从列序号'0'读取无效。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!