日期:2014-05-18  浏览次数:20923 次

[求助]datetimepicker数据怎么写入datatime字段数据库
string alarmTime = this.alarmDTP.Text;
string writeTime = this.writeDTP.Text;
string insertSql = "insert into diary(writetime,alarmtime) values(" + String.Format(writeTime, "yyyy\\mm\\dd HH:mm:ss") + "," + String.Format(alarmTime, "yyyy\\mm\\dd HH:mm:ss");
运行出现系统时间处有语法错误!
怎么回事?
谢谢~~


------解决方案--------------------
string insertSql = "insert into diary(writetime,alarmtime) values( convert(datetime,'" + String.Format(writeTime, "yyyy-mm-dd HH:mm:ss") + "'),convert(datetime,'" + String.Format(alarmTime, "yyyy-mm-dd HH:mm:ss")+"') )";
------解决方案--------------------
C# code
string insertSql = "insert into diary(writetime) values('" + datetimepicker.Value.ToString + "')"

------解决方案--------------------
少了一对单引号['']...

加上就好了...

C# code
string insertSql = "insert into diary(writetime) values('" + datetimepicker.Value.ToString("yyyy-MM-dd HH:mm:ss") + "')";