我收到了以下代码:
var model = new List<LogModels>();
while (reader.Read())
{
var logContent = new LogModels(
(int)reader["id"],
(string)reader["response"],
(string)reader["language"],
(string)reader["country"],
(string)reader["location"],
(string)reader["os"],
(string)reader["browser"],
(string)reader["type"],
(DateTime)reader["date"]
);
model.Add(logContent);
}
但是我收到以下错误:
An exception of type 'System.InvalidCastException' occurred in Speed Services.dll but was not handled in user code
Additional information: Specified cast is not valid.
图片:https://i.gyazo.com/adf04b8e271bb53c93303d5774a48f80.png
我的模型(LogModels)看起来像这样:
public int id { get; set; }
public string response { get; set; }
public string language { get; set; }
public string country { get; set; }
public string location { get; set; }
public string os { get; set; }
public string browser { get; set; }
public string type { get; set; }
public DateTime date { get; set; }
有人知道问题是什么,因为我似乎无法找到问题吗?
答案 0 :(得分:0)
var model = new List<LogModels>();
while (reader.Read())
{
var logContent = new LogModels(
Convert.ToInt32(reader["id"].Tostring()),
(string)reader["response"],
(string)reader["language"],
(string)reader["country"],
(string)reader["location"],
(string)reader["os"],
(string)reader["browser"],
(string)reader["type"],
Convert.ToDateTime(reader["date"].Tostring())
);
model.Add(logContent);
}
由于整数或日期时间值而导致转换问题。
(int)reader["Id"]
代替Convert.ToInt32(reader["Id"].Tostring())
(DateTime)reader["date"]
代替Convert.ToDateTime(reader["date"].Tostring())