繁体   English   中英

无法将类型“ void”隐式转换为“ string”

[英]Cannot implicitly convert type 'void' to 'string'

我已经尝试了一段时间,但我真的不明白。 我收到错误消息“无法将类型'void'隐式转换为'string'”我尝试了string,int,nothing,void,public,static和nope的多个变体,但我确实做得不好。

我想从DAL和BLL的数据库中获取一个值,我的代码如下所示。

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Repeater1.DataSource = BLL.getGames();
        Repeater1.DataBind();
        var culture = CultureInfo.GetCultureInfo("sv-SE");
        var dateTimeInfo = DateTimeFormatInfo.GetInstance(culture);
        var dateTime = DateTime.Today;
        int weekNumber = culture.Calendar.GetWeekOfYear(dateTime, dateTimeInfo.CalendarWeekRule, dateTimeInfo.FirstDayOfWeek);
        string mroundY = dateTime.Year.ToString();
        string mroundW = weekNumber.ToString();
        string mrounddate = mroundY + mroundW;

        string mround = BLL.getMroundGames(mrounddate);  <-- Here's the error

    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
}

我的BLL看起来像这样;

    public class BLL
{
    public static void getMroundGames(string mrounddate)
    {
        SqlCommand getMroundGames = new SqlCommand("SELECT mround FROM gameTB where mround = @mrounddate");
        DAL.ExecuteNonQuery(getMroundGames);
     }
}

也尝试过

public class BLL
{
    public static DataTable getMroundGames(string mrounddate)
    {
        SqlCommand getMroundGames = new SqlCommand("SELECT mround FROM gameTB where mround = @mrounddate");
        getMroundGames.Parameters.Add("@mrounddate", SqlDbType.VarChar, 10).Value = mrounddate;
        return DAL.GetData(getMroundGames);
    }
}

最后,我的DAL看起来像这样;

public class DAL
    {
    public static SqlConnection GetConnection()
        {
            SqlConnection conn = new
            SqlConnection(ConfigurationManager.ConnectionStrings["tiptopConnectionString"].ConnectionString);
            conn.Open();
            return conn;
        }

    public static DataTable GetData(SqlCommand command)
    {
        try
        {
            using (SqlConnection conn = GetConnection())
            {
                using (DataSet ds = new DataSet())
                {
                    using (SqlDataAdapter da = new SqlDataAdapter())
                    {
                        da.SelectCommand = command;
                        da.SelectCommand.Connection = conn;
                        da.Fill(ds);
                        return ds.Tables[0];
                    }
                }
            }
        }
        catch (Exception err)
        {
            throw new ApplicationException(string.Format("Felmeddelande:{0}", err.Message));
        }
    }

    public static object ExecuteScalar(SqlCommand command)
    {
        using (SqlConnection conn = GetConnection())
        {
            command.Connection = conn;
            object result = command.ExecuteScalar();
            return result;
        }
    }

    public static void ExecuteNonQuery(SqlCommand command)
    {
        using (SqlConnection conn = GetConnection())
        {
            command.Connection = conn;
            command.ExecuteNonQuery();
        }
    }

}

从哪里开始?

签名是错误的。

public static void getMroundGames(string mrounddate)

您需要将其更改为类似于;

public static string getMroundGames(string mrounddate)

从DAL中检索字符串值,并相应地返回给使用者。

var dt = Dal.GetData();
return (string)dt.Rows[0]["field"];

但是,老实说,我不会将数据表从您的DAL传递到您的BLL。 我将直接返回字符串或引入DTO,然后通过BLL从DAL中将其填充回消费者。

您需要将字符串的返回类型添加到getMroundGameas(string mrounddate)。

目前尚不清楚DAL是什么类型的对象,但您可能还应该使用ExecuteReader方法http://msdn.microsoft.com/zh-cn/library/bb344397.aspx,而不是ExecuteNonQuery。 这将返回一个Reader,可以查询select语句返回的值。

最后,您应该返回该值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM