insert datetime value in sql database with c#

后端 未结 8 1722
死守一世寂寞
死守一世寂寞 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 <table> (<date_column>) 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.

    0 讨论(0)
  • 2020-11-30 06:25
    INSERT INTO <table> (<date_column>) VALUES ('1/1/2010 12:00')
    
    0 讨论(0)
提交回复
热议问题