我发现很多人都在询问这个错误,但我可以找到一个正确的答案并解释一下。
类:Update
public class Log
{
public int id { get; set; }
public string description { get; set; }
public string type { get; set; }
public string action { get; set; }
public DateTime date { get; set; }
public string fullname { set; get; }
public Log() {
}
public Log(LogDs.LogFileRow row)
{
this.id = row.id;
this.type = row.type;
this.action = row.action.ToString();
this.description = row.description.ToString();
this.date = row.date;
this.fullname = row.fullname;
}
}
Bc课程:
public class LogBC
{
LogDalc dalc = new LogDalc();
public List<Log> List(DateTime dateStart, DateTime dateEnd)
{
List<Log> listLog = new List<Log>();
LogDs.LogFileDataTable dt = dalc.Sel(dateStart, dateEnd);
for (int i = 0; i < dt.Count; i++)
{
Log exp = new Log(dt[i]);
listLog.Add(exp);
}
return listLog;
}
Dalc课程:
public class LogDalc: BaseDalc
{
public LogDs.LogFileDataTable Sel(DateTime dateStart, DateTime dateEnd)
{
LogDs ds = new LogDs();
using (SqlConnection con = new SqlConnection(Global.Config.ConnStr))
{
SqlCommand cmd = new SqlCommand("spp_txn_expenses_sel", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@sdate", dateStart == DateTime.MinValue ? dbNull : dateStart));
cmd.Parameters.Add(new SqlParameter("@edate", dateEnd == DateTime.MinValue ? dbNull : dateEnd));
FillDataSet(cmd, ds, new string[] { ds.LogFile.TableName });
}
return ds.LogFile;
}
}
错误显示在Bc类中,完全在Log exp = new Log(dt[i]);
中。任何人都可以解释为什么会发生这种情况以及如何解决它?
答案 0 :(得分:0)
Log
类上没有构造函数接受单个参数。
提供参数化构造函数:
public class Log
{
public int id { get; set; }
public string description { get; set; }
public string type { get; set; }
public string action { get; set; }
public DateTime date { get; set; }
public string fullname { set; get; }
public Log(object myValue) {
// your logic for myValue assignment to some property
}
}
或使用属性的内联分配
Log exp = new Log() { Description = dt[i].ToString() };
答案 1 :(得分:0)
您的Log
课程不知道如何处理您传递的参数。您需要定义一个采用这种参数的构造函数:
public Log(LogDs.LogFileDataTable dt)
{
// initialize your settings from the DataTable here
}