无法将类型'object'隐式转换为'System.Data.OleDb.OleDb.DataReader' [英] cannot implicitly convert type 'object' to 'System.Data.OleDb.OleDb.DataReader'

查看:256
本文介绍了无法将类型'object'隐式转换为'System.Data.OleDb.OleDb.DataReader'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我修改了我的代码并摆脱了''DB''错误。但是,现在我有了新的错误;

无法将类型'对象'隐式转换为'System.Data.OleDb.OleDb.DataReader'存在显式转换(您是否缺少演员?)



代码:

 使用系统; 
使用 System.Web;
使用 System.Web.Profile;
使用 System.Web.UI;
使用 System.Data.OleDb;
使用 System.Data.SqlClient;
使用 System.Web.UI.HtmlControls;
使用 System.Web.UI.WebControls;

public partial class MasterPage2:MasterPage
{
protected void Page_Load( object sender,EventArgs e)
{
this .litdate.Text = DateTime.Now。 ToString( D);
if this .Page.User.Identity.Name.ToString()!= < span class =code-string>
{
string strSQL = string .Format( 选择来自AgentBasicInfo的Agent_current_bal,其中Agent_ID ='{0}' .Page.User.Identity.Name.ToString()。ToLower())。的ToString();

OleDbConnection conn = new OleDbConnection( ASPNETDB);
OleDbCommand cmd = new OleDbCommand(strSQL,conn);
conn.Open();
OleDbDataReader dr1 = cmd.ExecuteScalar();
.litbal.Text = dr1;
}
其他
{
.up12 .Visible = false ;
}
.submenu.Visible = .Page.User.Identity .Name.ToString()== admin;
this .lblCreatedBy.Text = 已创建作者:Christopher Mutoborokho;
}

}

解决方案

cmd.ExecuteScalar(); 无法转换为OleDbDataReader - 您尝试返回单个值而不是datareader,因此请使用类似

  float  dr1 =( float )cmd.ExecuteScalar(); 



你还需要将行 this.litbal.Text = dr1; 更改为

 this.litbal.Text = dr1.ToString(); 





使用

 OleDbDataReader dr1 = cmd.ExecuteReader(); 


您需要更改此行:



 OleDbDataReader dr1 = cmd.ExecuteScalar(); 





到此:



 OleDbDataReader dr1 = cmd.ExecuteReader ()





这将返回select语句中的行。然后你必须将这个阅读器加载到这样的数据表中:



 DataTable dt = new DataTable(); 
dt.Load(dr1);



然后,您可以遍历数据表行以获取所需的数据。


I have modified my code and got rid of the ''DB'' error. However, now i have a new errors;

"cannot implicitly convert type 'object' to 'System.Data.OleDb.OleDb.DataReader' An explicit conversion exists(are you missing a cast?)"


The code:

using System;
using System.Web;
using System.Web.Profile;
using System.Web.UI;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class MasterPage2 : MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.litdate.Text = DateTime.Now.ToString("D");
        if (this.Page.User.Identity.Name.ToString() != "")
        {
            string strSQL = string.Format("Select Agent_current_bal from AgentBasicInfo where Agent_ID='{0}'", this.Page.User.Identity.Name.ToString().ToLower()).ToString();

            OleDbConnection conn = new OleDbConnection("ASPNETDB");
 OleDbCommand cmd = new OleDbCommand(strSQL, conn);
 conn.Open();
 OleDbDataReader dr1 = cmd.ExecuteScalar();
            this.litbal.Text = dr1;
        }
        else
        {
            this.up12.Visible = false;
        }
        this.submenu.Visible = this.Page.User.Identity.Name.ToString() == "admin";
        this.lblCreatedBy.Text = "Created By: Christopher Mutoborokho";
    }

}

解决方案

cmd.ExecuteScalar(); cannot be cast to an OleDbDataReader - you are trying to return a single value not a datareader so use something like

float dr1 = (float)cmd.ExecuteScalar();


you will also need to change the line this.litbal.Text = dr1; to

this.litbal.Text = dr1.ToString();


OR
use

OleDbDataReader dr1 = cmd.ExecuteReader();


You need to change this line:

OleDbDataReader dr1 = cmd.ExecuteScalar();



to this:

OleDbDataReader dr1 = cmd.ExecuteReader()



That will return the rows from your select statement. Then you will have to load that reader into a datatable like this:

DataTable dt = new DataTable();
 dt.Load(dr1);


You can then iterate through your datatable rows to get the data you want.


这篇关于无法将类型'object'隐式转换为'System.Data.OleDb.OleDb.DataReader'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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