我已经尝试了一段时间了,我真的不明白。我发现错误“无法将类型'void'隐式转换为'string'” 我尝试了多种字符串变体,int,nothing,void,public,static和nope我真的不对。
我想通过我的DAL和BLL从我的db中获取一个值,我的代码看起来像这样。
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();
}
}
}
从哪里开始?
答案 0 :(得分:10)
签名是错误的;
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,然后再回传给消费者。
答案 1 :(得分:1)
您需要为getMroundGameas(string mrounddate)添加返回类型的字符串。
目前尚不清楚DAL的对象类型,但您也应该使用ExecuteReader方法,http://msdn.microsoft.com/en-us/library/bb344397.aspx 而不是ExecuteNonQuery。这将返回一个Reader,可以查询select语句返回的值。
最后你应该返回值。