C# 将DateTime从C插入Sql Server 2008#
我已经尝试了超过2个小时,所以非常感谢您的帮助C# 将DateTime从C插入Sql Server 2008#,c#,sql,datetime,sql-server-2008-r2,datetime-format,C#,Sql,Datetime,Sql Server 2008 R2,Datetime Format,我已经尝试了超过2个小时,所以非常感谢您的帮助 public void setAppointment(int studentID, DateTime appt) { connection.Open(); string sqlStatement3 = "UPDATE dbo.students SET appointmentDate = '" + appt.Date.ToString("yyyy-MM-dd HH:mm:ss") + "' WHERE I
public void setAppointment(int studentID, DateTime appt)
{
connection.Open();
string sqlStatement3 = "UPDATE dbo.students SET appointmentDate = '" + appt.Date.ToString("yyyy-MM-dd HH:mm:ss") + "' WHERE ID = " + studentID + ";";
OleDbCommand updateCommand = new OleDbCommand(sqlStatement3, connection);
updateCommand.ExecuteNonQuery();
connection.Close();
}
因此,基本上,这就是在sql server表中插入日期时间,保持月和日的格式不变,以避免区域设置的影响
唯一的问题是时间仍然是00:00:00。即使在调试代码时,“appt”显示2013年6月28日09:30:00第5行。
改变
到
试试下面
public void setAppointment(int studentID, DateTime appt)
{
connection.Open();
string sqlStatement3 = "UPDATE dbo.students SET appointmentDate = ? WHERE ID = ?";
OleDbCommand updateCommand = new OleDbCommand(sqlStatement3, connection);
updateCommand.Parameters.AddWithValue("@p1", appt);
updateCommand.Parameters.AddWithValue("@p2", studentID);
updateCommand.ExecuteNonQuery();
connection.Close();
}
但是强>
您说它是sql server,但为什么要使用OleDbCommand
如果是sql server,请尝试下面的内容
public void setAppointment(int studentID, DateTime appt)
{
using (SqlConnection con = new SqlConnection(connectionString))
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "UPDATE dbo.students SET appointmentDate = @appointmentDate WHERE ID = @ID";
con.Open();
cmd.Parameters.AddWithValue("@appointmentDate", appt);
cmd.Parameters.AddWithValue("@ID", studentID);
cmd.ExecuteNonQuery();
}
}
我希望您已经解决了上一篇文章中的问题,并且我同意将SQL语句与参数一起使用 如果应用程序的日期时间格式是固定的,那么硬编码并没有坏处,但从web.config文件中获取日期时间格式将是一个很好的代码。这将帮助您的代码在整个项目中保持一致 而不是 ToString(“yyyy-MM-dd HH:MM:ss”) ToString(配置值)
太晚了,但是对于你的问题:试试下面的代码
public void setAppointment(int studentID, DateTime appt)
{
connection.Open();
string sqlStatement3 = "UPDATE dbo.students SET appointmentDate = '" + "CONVERT(datetime, '" + appt.Date.ToString("yyyy-MM-dd HH:mm:ss") + "', 103)" + "' WHERE ID = " + studentID + ";";
OleDbCommand updateCommand = new OleDbCommand(sqlStatement3, connection);
updateCommand.ExecuteNonQuery();
connection.Close();
}
-您不应该将SQL语句连接在一起-而是使用参数化查询来避免SQL注入+1,这是唯一安全的方法-是的,对于SQL Server来说,使用
SqlConnection/SqlCommand
比使用OleDb更有意义…第二个答案-带参数-工作起来很有魅力。我以前使用的是非参数化查询,我对日期有各种各样的问题。使用参数,没有问题。如下所示的参数化查询既安全又快速。你应该养成经常这样做的习惯——你不太可能遇到int和datetime的问题,但是当你将其用于字符串值时,你就引入了SQL注入漏洞。
public void setAppointment(int studentID, DateTime appt)
{
using (SqlConnection con = new SqlConnection(connectionString))
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "UPDATE dbo.students SET appointmentDate = @appointmentDate WHERE ID = @ID";
con.Open();
cmd.Parameters.AddWithValue("@appointmentDate", appt);
cmd.Parameters.AddWithValue("@ID", studentID);
cmd.ExecuteNonQuery();
}
}
public void setAppointment(int studentID, DateTime appt)
{
connection.Open();
string sqlStatement3 = "UPDATE dbo.students SET appointmentDate = '" + "CONVERT(datetime, '" + appt.Date.ToString("yyyy-MM-dd HH:mm:ss") + "', 103)" + "' WHERE ID = " + studentID + ";";
OleDbCommand updateCommand = new OleDbCommand(sqlStatement3, connection);
updateCommand.ExecuteNonQuery();
connection.Close();
}