insert datetime value in sql database with c#

后端 未结 8 1760
死守一世寂寞
死守一世寂寞 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:08

    you can send your DateTime value into SQL as a String with its special format. this format is "yyyy-MM-dd HH:mm:ss"

    Example: CurrentTime is a variable as datetime Type in SQL. And dt is a DateTime variable in .Net.

    DateTime dt=DateTime.Now;
    string sql = "insert into Users (CurrentTime) values (‘{0}’)";
    
    sql = string.Format(sql, dt.ToString("yyyy-MM-dd HH:mm:ss") );
    

提交回复
热议问题