插入到数据库最新与另一种格式 [英] Insert into database date with another format
问题描述
我有一个GridView和SqlDataSource的。
I have a gridview and sqldatasource.
我想在数据库中插入新记录,日期列类型是日期和默认格式为美国,MM / DD / YYYY,我试图插入与其他格式的日期。
I'm trying to insert new records in database and the Date column type is Date and default format is US , mm/dd/yyyy and I'm trying to insert a date with another format.
SqlDataSource2.InsertCommand = "Insert into test (Name,Date) VALUES (@Name,@CONVERT(Date,'@Date',104))";
SqlDataSource2.InsertParameters.Add("Name", nameText);
SqlDataSource2.InsertParameters.Add("Date", DbType.DateTime, date.Text);
我得到:字符串未被识别为有效的DateTime。
I get: String was not recognized as a valid DateTime.
感谢
推荐答案
日期类型在数据库格式无关(除非你将其存储为一个字符串)。
The Date type in format agnostic in the database (unless you store it as a string).
您可以先试着得到一个有效的使用对象的System.DateTime
DateTime.ParseExact
或 DateTime.Parse
。
You may first try to get a valid System.DateTime
object using DateTime.ParseExact
or DateTime.Parse
.
然后,这个值设置的SqlParameter。
Then, set the SqlParameter with this value.
DateTime dateValue = DateTime.Parse(date.Text); // try to make this working first.
SqlDataSource2.InsertCommand= "Insert into test (Name,Date) VALUES (@Name,@Date)";
SqlDataSource2.InsertParameters.Add("Name", nameText);
SqlDataSource2.InsertParameters.Add("Date", DbType.DateTime, dateValue);
这篇关于插入到数据库最新与另一种格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!