insert datetime value in sql database with c#

后端 未结 8 1779
死守一世寂寞
死守一世寂寞 2020-11-30 05:29

How do I insert a datetime value into a SQL database table where the type of the column is datetime?

8条回答
  •  佛祖请我去吃肉
    2020-11-30 06:14

    using (SqlConnection conn = new SqlConnection())
    using (SqlCommand cmd = conn.CreateCommand())
    {
        cmd.CommandText = "INSERT INTO  () VALUES ('2010-01-01 12:00')";
        cmd.ExecuteNonQuery();
    }
    
    
    

    It's been awhile since I wrote this stuff, so this may not be perfect. but the general idea is there.

    WARNING: this is unsanitized. You should use parameters to avoid injection attacks.

    EDIT: Since Jon insists.

    提交回复
    热议问题