0 c# sql datetime datetime-format sql-server-2008-r2
我一直试图让这个权利超过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 ID = " + studentID + ";";
OleDbCommand updateCommand = new OleDbCommand(sqlStatement3, connection);
updateCommand.ExecuteNonQuery();
connection.Close();
}
Run Code Online (Sandbox Code Playgroud)
所以基本上做的是将日期时间插入到sql server表中,保持月和日的相同格式,以避免区域设置妨碍.
唯一的问题是时间仍然是00:00:00.即使我调试代码时,'appt'显示28/06/2013 09:30:00
Dam*_*ith 10
试试下面
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();
}
Run Code Online (Sandbox Code Playgroud)
但!
你说它是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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16660 次 |
| 最近记录: |