我试图将数据从excel转换为数据库中我的字段的类型,但我收到了上述错误:
string query = "Insert into Réception_camions([Date d'arrivée],[heure d'arrivée],Poids_cam,Id_cam,Id_qualité) Values('" +DateTime.Parse(ds.Tables[0].Rows[i][0].ToString()) + "','" + /*TimeSpan.Parse*/(ds.Tables[0].Rows[i][1].ToString()) + "','" + ds.Tables[0].Rows[i][2].ToString() + "','" + ds.Tables[0].Rows[i][3].ToString() + "','" + ds.Tables[0].Rows[i][4].ToString() + "')";
还有其他方法可以转换为datetime吗?
答案 0 :(得分:0)
SQL Server有时不接受某些字符串,其中日期和时间值之间有空格。您可以通过使用' T'替换空格来解决此问题。字符。
--Failure
INSERT INTO TableName (DateTimeVal) VALUES ('2018-08-09 12:19:28')
--Success
INSERT INTO TableName (DateTimeVal) VALUES ('2018-08-09T12:19:28')
但是,我强烈建议您遵循其他答案的建议,找到将数据插入数据库的更好方法