在SQL数据库中使用c#插入datetime值

问题描述:

如何将datetime值插入SQL数据库表,其中列的类型是datetime?

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

以下应该工作,是我的建议(参数化查询):

The following should work and is my recommendation (parameterized query):

DateTime dateTimeVariable = //some DateTime value, e.g. DateTime.Now;
SqlCommand cmd = new SqlCommand("INSERT INTO <table> (<column>) VALUES (@value)", connection);
cmd.Parameters.AddWithValue("@value", dateTimeVariable);

cmd.ExecuteNonQuery();