本文介绍了CS0266:无法将类型'object'隐式转换为'System.Data.SqlClient.SqlDataReader'。这个错误导致行dr = cmd.executescalar()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public int membercount(string tbl, string condition)
    {
        string query = null;
        if (condition.Trim().Length > 0) 
        {
            query = query + "where" + condition;
        }
        else
        {
            query = query;
        }
        cmd = new SqlCommand(query, con);
       dr= cmd.ExecuteScalar();
        return a;   
    }

推荐答案

public int membercount(string tbl, string condition)
{
    string query = "SELECT COUNT(*) FROM " + tbl; //Since your function is for counting member.
    if (condition.Trim().Length > 0)
    {
        query = query + " where " + condition;
    }
    cmd = new SqlCommand(query, con);
    int a = Convert.ToInt32(cmd.ExecuteScalar());//Converting the returned value to Int32
    return a;
}







- Amit



这篇关于CS0266:无法将类型'object'隐式转换为'System.Data.SqlClient.SqlDataReader'。这个错误导致行dr = cmd.executescalar()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 07:57