将日期从日历添加到SQL server 2008
本文关键字:SQL server 2008 添加 日期 日历 | 更新日期: 2023-09-27 17:50:45
我正在asp.net c#中的一个项目中工作,我从两个日历中获取日期,并将它们存储在指定为SprintsN表中的datetime的数据库字段中,sql server 2008数据库。但是我得到了一个异常
例外:
insert into SprintsN (S_Duration, s_status, P_ID StartDate,EstEndDate)值("1周","不完整","5","2015-05-01 12:00:00’,‘2015-05-0712:00:00");System.Data.SqlClient。SqlException (0x80131904):错误"开始日期"附近的语法。在System.Data.SqlClient.SqlConnection。OnError (SqlException异常,breakConnection, Action ' 1 wrapCloseInAction) at
代码:
日历事件:
protected void Calendar2_SelectionChanged(object sender, EventArgs e)
{
EstDate = Calendar2.SelectedDate.ToString("yyyy-MM-dd hh:mm:ss");
EstDateBox.Text = EstDate;
Calendar2.Visible = false;
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
startdate = Calendar1.SelectedDate.ToString("yyyy-MM-dd hh:mm:ss");
SDate.Text = startdate;
Calendar1.Visible = false;
}
Sql数据库存储处理:
String query="";
try {
query = "insert into SprintsN (S_Duration, S_Staus, P_ID StartDate,EstEndDate) values('" + SprintDurBox.Text + "', 'incomplete','" + p_ID + "','"+startdate+"','"+EstDate+"');";
SqlConnection con = new SqlConnection(str);
SqlCommand com = new SqlCommand(query, con);
con.Open();
if (com.ExecuteNonQuery() == 1)
{
SprintStatus.Text = "Sprint Successfully Saved";
}
else
{
SprintStatus.Text = "Sprint not Saved";
}
}
catch (Exception ex)
{
Response.Write(query);
Response.Write(ex.ToString());
}
在你的查询的第一部分,看起来你少了一个逗号。
insert into SprintsN (S_Duration, S_Staus, P_ID StartDate,EstEndDate)
应为:
insert into SprintsN (S_Duration, S_Staus, P_ID, StartDate,EstEndDate)