[英]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.