簡體   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