C# 无法将类型“void”隐式转换为“string”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15260241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot implicitly convert type 'void' to 'string'
提问by Slint
I've tried for a while now and I really dont get it. I recive error "Cannot implicitly convert type 'void' to 'string'" I have tried multiple variants of string, int, nothing, void, public, static and nope I really dont get it right.
我已经尝试了一段时间,但我真的不明白。我收到错误“无法将类型 'void' 隐式转换为 'string'” 我尝试了字符串、int、nothing、void、public、static 和 nope 的多种变体,我真的不明白。
I want to get one value from my db thoug my DAL and BLL, my code looks like this.
我想从我的数据库中获取一个值,包括我的 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)
{
}
}
My BLL looks like this;
我的 BLL 看起来像这样;
public class BLL
{
public static void getMroundGames(string mrounddate)
{
SqlCommand getMroundGames = new SqlCommand("SELECT mround FROM gameTB where mround = @mrounddate");
DAL.ExecuteNonQuery(getMroundGames);
}
}
Also tried this;
也试过这个;
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);
}
}
And finaly my DAL looks like this;
最后我的 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();
}
}
}
Where to begin?
从哪里开始?
回答by ChrisBint
The signature for this is wrong;
签名是错误的;
public static void getMroundGames(string mrounddate)
You need to change it to something similar to ;
您需要将其更改为类似于 ;
public static string getMroundGames(string mrounddate)
Retrieve the string value from your DAL and return to the consumer accordingly.
从您的 DAL 检索字符串值并相应地返回给使用者。
var dt = Dal.GetData();
return (string)dt.Rows[0]["field"];
However, in all honesty, I would not be passing a datatable from your DAL to your BLL. I would return the string directly or introduce a DTO and populate this from your DAL through your BLL, back to the consumer.
但是,老实说,我不会将数据表从您的 DAL 传递到您的 BLL。我会直接返回字符串或引入 DTO 并通过 BLL 从您的 DAL 填充它,返回给消费者。
回答by alanh
You need to add a return type of string to getMroundGameas(string mrounddate).
您需要向 getMroundGameas(string mrounddate) 添加字符串的返回类型。
It's not clear what type of object DAL is, but you should also probably use ExecuteReader method, http://msdn.microsoft.com/en-us/library/bb344397.aspxrather then ExecuteNonQuery. This will return a Reader which can be queried for the value returned by the select statement.
不清楚 DAL 是什么类型的对象,但您可能还应该使用 ExecuteReader 方法,http://msdn.microsoft.com/en-us/library/bb344397.aspx而不是 ExecuteNonQuery。这将返回一个 Reader,可以查询 select 语句返回的值。
Finally you should return the value.
最后你应该返回值。