简体   繁体   中英

Insert DateTime into Sql Server 2008 from C#

I've been trying to get this right for over 2hrs so any help is highly appreciated

  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();
    }

So basically what that does is insert a datetime into an sql server table keeping the same format of the month and day to avoid regional settings getting in the way.

The only problem is that the time remains 00:00:00. Even though when I debug the code, 'appt' shows 28/06/2013 09:30:00

try below

 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();
        }

BUT!

You say it is sql server but why you using OleDbCommand ?

try below if it is 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();
    }
}

Line 5. Change

...  appt.Date.ToString(...

to

... appt.ToString(...

I hope you have solved your problem from previous post and I agree SQL Statements to be used with parameters.

If you have an application date time format is fixed, then there is no harm in hard-coding but it would be good code to get date time format from your web.config file. This will help your code to be same consistent overall project.

Instead of

ToString("yyyy-MM-dd HH:mm:ss")

ToString(ConfigValue)

Too Late, But for your question : Try the code below.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM