C# 从字符串#2转换日期和/或时间时,转换失败
这是我的密码:C# 从字符串#2转换日期和/或时间时,转换失败,c#,ado.net,sqlparameter,C#,Ado.net,Sqlparameter,这是我的密码: 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.ToDate
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的值如下:
问题:我的
DateTime
对象是否存在SQL不喜欢的问题?如果没有,发生了什么事???您依赖于列的顺序位置。。。这可能是因为它们与相应值的顺序不同。您可以尝试显式命名目标列。我在猜这里的列名
insert into actor
(Name, Biography, PlaceOfBirth, ProfilePath, DoB, DoD)
values
(@name, @biography, @placeOfBirth, @profilePath, @dob, @dod)
插入到actor中
(姓名、传记、出生地点、档案路径、国防部、国防部)
价值观
(@name、@biography、@placeOfBirth、@profilePath、@dob、@dod)
您依赖于列的顺序位置。。。这可能是因为它们与相应值的顺序不同。您可以尝试显式命名目标列。我在猜这里的列名
insert into actor
(Name, Biography, PlaceOfBirth, ProfilePath, DoB, DoD)
values
(@name, @biography, @placeOfBirth, @profilePath, @dob, @dod)
插入到actor中
(姓名、传记、出生地点、档案路径、国防部、国防部)
价值观
(@name、@传记、@placeOfBirth、@profilePath、@dob、@dod)
我已经编辑了你的标题。请看“”,其中的共识是“不,他们不应该”。我已经编辑了你的标题。请看,“,共识是“不,他们不应该”。@MikeMarks怎么样了?@MikeMarks怎么样了?