DataReader不兼容 [英] The DataReader Is Incompatible

查看:774
本文介绍了DataReader不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了EF模型,然后在一个类中我写了以下代码来检索DataBase的值。并将值存储在另一个表中。但是它给我的错误DATAREADER IS INCompatable如下所述。

I have created the EF model and then in a Class I have written the following Code to retrieve the value form the DataBase. And store the value in another Table. But it gives me the Error "DATAREADER IS INCompatable" as explained Below..

      EmpRole empr = new EmpRole();
        empr.EmpId = strEmpId;
      string str="select RoleId from RoleName where roleName like '"+strDesignation+"'";
      var context= DbAccess.Data.ExecuteStoreQuery<RoleName>(str, null);  //Here Showing Error

        empr.RoleId = Convert.ToInt16(context);
        DbAccess.Data.EmpRoles.AddObject(empr);
        DbAccess.Commit();

显示错误,如:

DataTable是:

DataTables were:

CREATE TABLE [dbo].[RoleName](
    [SNo] [int] IDENTITY(1,1) NOT NULL,
    [RoleId] [smallint] NOT NULL,
    [RoleName] [varchar](50) NULL,
 CONSTRAINT [PK_RoleName] PRIMARY KEY CLUSTERED 
(
    [RoleId] ASC
)

CREATE TABLE [dbo].[EmpRoles](
    [Sno] [int] IDENTITY(1,1) NOT NULL,
    [EmpId] [varchar](8) NOT NULL,
    [RoleId] [smallint] NOT NULL,
    [ReportingToId] [varchar](8) NULL,
 CONSTRAINT [PK_EmpRoles] PRIMARY KEY CLUSTERED 
(
    [Sno] ASC
)

数据读取器与指定的MyOrgDBModel.RoleName不兼容,类型为SNo的成员在数据读取器中没有相同的列。

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

请告诉我执行sqlQuery的原因。

Please tell me the reasons what to do to execute the sqlQuery.

推荐答案

这是因为您没有在选择查询中选择SNo列。当你在RoleName中填充并且它的属性SNo列应该存在于数据读取器中。如果您只想要将RoleId置于查询中,则使用一个属性RoleId创建新类型并使用此类型。创建新的类型如下

This is because you are not selecting the SNo column in your select query. As you are populating in RoleName and it has property SNo column should be present in data reader. If you just want the RoleId to be in query then create new type with one property RoleId and use this. Create new type like below

public class CustomRoleName
{
    int RoleId { get; set; }
}

现在更改您的代码如下

EmpRole empr = new EmpRole();
empr.EmpId = strEmpId;
string str="select RoleId from RoleName where roleName like '"+strDesignation+"'";
foreach(CustomRoleName rn in  DbAccess.Data.ExecuteStoreQuery<CustomRoleName>(str))
{
     empr.RoleId = rn.RoleId ;
     DbAccess.Data.EmpRoles.AddObject(empr);
     DbAccess.Commit();
     break;
}

这篇关于DataReader不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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