无法将类型'int'隐式转换为'System.Data.SqlClient.SqlDataReader'

时间:2022-02-04 16:34:50

I am trying to use a SqlDataReader to count the amount of categories I use.

我正在尝试使用SqlDataReader来计算我使用的类别数量。

Here is my Business Logic Code:

这是我的业务逻辑代码:

// Return count of main categories for homepage
    [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
    public int GetMainCatCount(int intCategoryID)
    {
        intCategoryID = SQLInject(intCategoryID);

        SqlConnection con = new SqlConnection(conString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT COUNT(intCategoryID) "
                        + "FROM  tblCategory "
                        + "WHERE intCategoryID=" + intCategoryID;
        con.Open();
        return (Int32)cmd.ExecuteScalar();
    }

and here is my code in the code benhind page making the call:

这是调用代码背后页面中的代码:

public string CountCategory(int intCategoryID)
    {
        SqlDataReader myReader;
        myReader = CategoryBLL.GetMainCatCount(intCategoryID);

        myReader.Close();
        myReader.Dispose();
        return Convert.ToInt32(myReader);
    }

I want to use the results of the SqlDataReader to populate a label tag. When I try and run this code I recieve this error message:

我想使用SqlDataReader的结果来填充标签标签。当我尝试运行此代码时,我收到此错误消息:

Cannot implicitly convert type 'int' to 'System.Data.SqlClient.SqlDataReader'

无法将类型'int'隐式转换为'System.Data.SqlClient.SqlDataReader'

Could anyone please tell me where I am going wrong. Thanks...

谁能告诉我哪里出错了?谢谢...

3 个解决方案

#1


Could anyone please tell me where I am going wrong. Thanks...

谁能告诉我哪里出错了?谢谢...

Sure: you're going wrong trying to convert an int to an SqlDataReader. You're also trying to convert an SqlDataReader to an int, later on.

当然:你试图将int转换为SqlDataReader是错误的。您还尝试将SqlDataReader转换为int,稍后再进行。

Honestly, the types in your example are so screwed up that I can't even figure out what the code is supposed to do. To get started, read this.

老实说,你的例子中的类型是如此搞砸,我甚至无法弄清楚代码应该做什么。要开始,请阅读此内容。


Thinking more about it, here's a potential fixed version:

更多地考虑它,这是一个潜在的固定版本:

public string CountCategory(int intCategoryID)
{
    int count = CategoryBLL.GetMainCatCount(intCategoryID);
    return count.ToString();
}

#2


Your "GetMainCatCount" method returns an int, but you're assigning its return value to a variable of type SqlDataReader:

您的“GetMainCatCount”方法返回一个int,但您将其返回值分配给SqlDataReader类型的变量:

SqlDataReader myReader;
myReader = CategoryBLL.GetMainCatCount(intCategoryID);

Additionally, your CountCategory method is defined as returning a string, yet you're returning an Int32 from it (the result of a Convert.ToInt32 call on the last line).

此外,您的CountCategory方法被定义为返回一个字符串,但您从它返回一个Int32(最后一行上的Convert.ToInt32调用的结果)。

#3


 myReader = CategoryBLL.GetMainCatCount(intCategoryID);

In that line, you're trying to convert an Int32 to an object of Type SqlDataReader.

在该行中,您尝试将Int32转换为Type SqlDataReader的对象。

You want to try myReader.GetMainCatCount(intCatagorID);

你想尝试myReader.GetMainCatCount(intCatagorID);

#1


Could anyone please tell me where I am going wrong. Thanks...

谁能告诉我哪里出错了?谢谢...

Sure: you're going wrong trying to convert an int to an SqlDataReader. You're also trying to convert an SqlDataReader to an int, later on.

当然:你试图将int转换为SqlDataReader是错误的。您还尝试将SqlDataReader转换为int,稍后再进行。

Honestly, the types in your example are so screwed up that I can't even figure out what the code is supposed to do. To get started, read this.

老实说,你的例子中的类型是如此搞砸,我甚至无法弄清楚代码应该做什么。要开始,请阅读此内容。


Thinking more about it, here's a potential fixed version:

更多地考虑它,这是一个潜在的固定版本:

public string CountCategory(int intCategoryID)
{
    int count = CategoryBLL.GetMainCatCount(intCategoryID);
    return count.ToString();
}

#2


Your "GetMainCatCount" method returns an int, but you're assigning its return value to a variable of type SqlDataReader:

您的“GetMainCatCount”方法返回一个int,但您将其返回值分配给SqlDataReader类型的变量:

SqlDataReader myReader;
myReader = CategoryBLL.GetMainCatCount(intCategoryID);

Additionally, your CountCategory method is defined as returning a string, yet you're returning an Int32 from it (the result of a Convert.ToInt32 call on the last line).

此外,您的CountCategory方法被定义为返回一个字符串,但您从它返回一个Int32(最后一行上的Convert.ToInt32调用的结果)。

#3


 myReader = CategoryBLL.GetMainCatCount(intCategoryID);

In that line, you're trying to convert an Int32 to an object of Type SqlDataReader.

在该行中,您尝试将Int32转换为Type SqlDataReader的对象。

You want to try myReader.GetMainCatCount(intCatagorID);

你想尝试myReader.GetMainCatCount(intCatagorID);