代码之家  ›  专栏  ›  技术社区  ›  Sam

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

  •  27
  • Sam  · 技术社区  · 17 年前

    如何在列类型为datetime的SQL数据库表中插入datetime值?

    8 回复  |  直到 13 年前
        1
  •  90
  •   Thorsten Dittmar    9 年前

    以下是我的建议(参数化查询):

    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();
    
        2
  •  19
  •   Sean B    9 年前
     DateTime time = DateTime.Now;              // Use current time
     string format = "yyyy-MM-dd HH:mm:ss";    // modify the format depending upon input required in the column in database 
     string insert = @" insert into Table(DateTime Column) values ('" + time.ToString(format) + "')"; 
    

    并执行查询。 DateTime.Now 插入当前日期时间。。

        3
  •  7
  •   Andrea    17 年前

    使用格式更规范 年-月-日-时:分:秒 (即: )

    使用它,您就不必担心日期的格式(MM/DD/yyy或DD/MM/yyy)。这对所有人都有效。

        4
  •  2
  •   Joel    17 年前
    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();
    }
    

    我写这些东西已经有一段时间了,所以这可能并不完美。但总的想法是有的。

    警告:这是未初始化的。您应该使用参数来避免注入攻击。

    因为乔恩坚持。

        5
  •  1
  •   joonas    7 年前

    这是一个有着正确答案的老问题( please use parameterized queries datetime

    结果,他们根本没有。

    日期时间 列存储给定的 DateTime 原来如此,没有任何转换。给定的日期时间是UTC还是本地并不重要。

    你可以亲眼看到:

    using (var connection = new SqlConnection(connectionString))
    {
        connection.Open();
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT * FROM (VALUES (@a, @b, @c)) example(a, b, c);";
    
            var local = DateTime.Now;
            var utc = local.ToUniversalTime();
    
            command.Parameters.AddWithValue("@a", utc);
            command.Parameters.AddWithValue("@b", local);
            command.Parameters.AddWithValue("@c", utc.ToLocalTime());
    
            using (var reader = command.ExecuteReader())
            {
                reader.Read();
    
                var localRendered = local.ToString("o");
    
                Console.WriteLine($"a = {utc.ToString("o").PadRight(localRendered.Length, ' ')} read = {reader.GetDateTime(0):o}, {reader.GetDateTime(0).Kind}");
                Console.WriteLine($"b = {local:o} read = {reader.GetDateTime(1):o}, {reader.GetDateTime(1).Kind}");
                Console.WriteLine($"{"".PadRight(localRendered.Length + 4, ' ')} read = {reader.GetDateTime(2):o}, {reader.GetDateTime(2).Kind}");
            }
        }
    }
    

    这将打印什么当然取决于您的时区,但是 最重要的是,读取值将 Kind = Unspecified "o" format string (roundtrip)

    GMT+02:00输出示例:

    a = 2018-11-20T10:17:56.8710881Z      read = 2018-11-20T10:17:56.8700000, Unspecified
    b = 2018-11-20T12:17:56.8710881+02:00 read = 2018-11-20T12:17:56.8700000, Unspecified
                                          read = 2018-11-20T12:17:56.8700000, Unspecified
    

        6
  •  -1
  •   PedPak    9 年前

    您可以将DateTime值以字符串的形式发送到SQL中。此格式为“yyyy-MM-dd HH:MM:ss”

    例子: 当前时间 是一个变量 键入SQL。以及 日期 日期时间 .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") );
    
        7
  •  -3
  •   RomCoo    10 年前

    DateTime 反对 string 用单引号括起来。我认为它是什么格式并不重要,只要它是有效的。

    string data = "'" + date.ToString() = "'";
    

    data.ToString(format) ;

        8
  •  -7
  •   atfergs    17 年前
    INSERT INTO <table> (<date_column>) VALUES ('1/1/2010 12:00')