在数据读取器中没有具有相同名称的对应列 [英] does not have a corresponding column in the data reader with the same name

查看:55
本文介绍了在数据读取器中没有具有相同名称的对应列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 tbl_search ,其列为: id(int),title( nvarchar100),结果(ntext)
,我想使用 SQL查询,例如:

 使用(var db = new GoogleEntities())
{
const string selectCmd =
@从tbl_search中选择前1个标题其中title = @ title和id = @ id;

var data = db.Database.SqlQuery< tbl_search>(
selectCmd,
新SqlParameter( @ title, wcf),
新SqlParameter( @id,1)
).FirstOrDefault();



if(data!= null)
{
var serviceMember = data.ToString();
label1.Text = serviceMember ==吗? :(serviceMember == True吗? On: Off);
}
}

但它给我一个错误:


数据读取器与指定的 GoogleModel.tbl_search不兼容。类型 id的成员在数据读取器中没有具有相同名称的对应列。


注意:这是我的 tbl_search 类:

 公共局部类tbl_search 
{
public int id {get;组; }
公共字符串标题{get;组; }
公用字串结果{get;组; }
}

我有 id

解决方案

您的SQL语句仅返回标题而不是完整实体。 / p>

更改:

 从tbl_search中选择前1个标题title = @ title and id = @ id 

至:

 选择顶部1 *从tbl_search其中title = @ title和id = @ id 


I have Table named tbl_search with columns : id(int), title(nvarchar100), result(ntext) and i want using SQL query, like this :

    using (var db = new GoogleEntities())
      {
          const string selectCmd =
                @"Select top 1 title From tbl_search Where title=@title and id=@id ";

         var data = db.Database.SqlQuery<tbl_search>(
                selectCmd,
                new SqlParameter("@title", "wcf"),
                new SqlParameter("@id", 1)
                ).FirstOrDefault();



           if (data != null)
               {
                var serviceMember = data.ToString();
                label1.Text = serviceMember == "" ? "" : (serviceMember == "True" ? "On" : "Off");
               }
       }

but it give me an error :

The data reader is incompatible with the specified 'GoogleModel.tbl_search'. A member of the type, 'id', does not have a corresponding column in the data reader with the same name.

NOTE: this is my tbl_search class :

 public partial class tbl_search
{
    public int id { get; set; }
    public string title { get; set; }
    public string result { get; set; }
}

i have id in my table.. What is the problem!!

解决方案

Your SQL Statement only returns the title not the full entity.

Change:

Select top 1 title From tbl_search Where title=@title and id=@id 

to:

Select top 1 * From tbl_search Where title=@title and id=@id 

这篇关于在数据读取器中没有具有相同名称的对应列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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