[英]Conversion failed when converting date and/or time from character string #2
这是我的代码:
DateTime Dob = Convert.ToDateTime("1/1/1800");
DateTime Dod = Convert.ToDateTime("1/1/1800");
if (!string.IsNullOrEmpty(p.birthday))
Dob = Convert.ToDateTime(p.birthday);
if (!string.IsNullOrEmpty(p.deathday))
Dod = Convert.ToDateTime(p.deathday);
string query = string.Format("insert into actor values (@name, @biography, @placeOfBirth, @profilePath, @dob, @dod)");
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.Add(new SqlParameter("@name", !string.IsNullOrEmpty(p.name) ? p.name : "not available"));
command.Parameters.Add(new SqlParameter("@biography", !string.IsNullOrEmpty(p.biography) ? p.biography : "not available"));
command.Parameters.Add(new SqlParameter("@placeOfBirth", !string.IsNullOrEmpty(p.place_of_birth) ? p.place_of_birth : "not available"));
command.Parameters.Add(new SqlParameter("@profilePath", !string.IsNullOrEmpty(p.profile_path) ? p.profile_path : "not available"));
command.Parameters.Add(new SqlParameter("@dob", Dob));
command.Parameters.Add(new SqlParameter("@dod", Dod));
connection.Open();
command.ExecuteNonQuery();
我得到的错误是:
从字符串转换日期和/或时间时转换失败
Dod和Dob的值如下:
问题 :我不喜欢SQL的DateTime
对象出问题了吗? 如果没有,那是怎么回事???
您所依赖的是列的顺序位置...这可能与相应值的顺序不同。 您可以尝试显式命名目标列。 我猜这里的列名。
insert into actor
(Name, Biography, PlaceOfBirth, ProfilePath, DoB, DoD)
values
(@name, @biography, @placeOfBirth, @profilePath, @dob, @dod)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.