【问题标题】:Cannot implicitly convert type 'void' to 'string'无法将类型“void”隐式转换为“string”
【发布时间】:2013-02-22 00:07:17
【问题描述】:

我已经尝试了一段时间,但我真的不明白。我收到错误“无法将类型'void'隐式转换为'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();
        }
    }

}

从哪里开始?

【问题讨论】:

  • 你的函数是void,即什么也不返回。然后您尝试将其分配给字符串...您期望/打算发生什么?
  • 你为什么返回void?
  • 好吧,而不是让事情变得更难。您将如何对单个值进行简单的查询以显示在您的 aspx 页面上,其中 mround = 当年的当前周。所以sql会是;从 gameTB 中选择 mround,其中 mround = 201310 我将 sql 值获取到我的标签中,id 为 mroundlabel?

标签: c# data-access-layer bll


【解决方案1】:

这个签名是错误的;

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 填充它,然后返回给消费者。

【讨论】:

    【解决方案2】:

    你需要给getMroundGameas(string mrounddate)添加一个返回类型的字符串。

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

    最后你应该返回值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 2016-02-17
      • 2015-04-26
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多