简体   繁体   中英

PgSqlExcetion: date/time field value out of range: “4/20/2012 12:00:00 AM”

I want Insert Data Table in DataBase

    public void InsertNote(DataTable dataTable)
            {
                var query = new StringBuilder();

                query.Append("INSERT INTO sde.\"Table1\" 
(id, disciplol, date, second_id, note, key) VALUES ");

                foreach (DataRow row in dataTable.Rows)
                {
                    query.AppendFormat(" ({0}, '{1}', '{2}', {3}, '{4}', '{5}'),", 
row["classid"], row["disciplina"], row["datelesson"], row["student_id"], "", row["key"]);
                }

                // TODO : Review
                query.Replace(',', ';', query.Length - 2, 2);

                var cmd = new PgSqlCommand(query.ToString());

                var con = DB.Sqlconnection;
                cmd.Connection = con;
                con.Open();
                try
                {
                    cmd.ExecuteNonQuery();
                    // result = true;
                }
                catch (Exception ex)
                {
                    //result = false;
                    log.Error("InsertInTable1", ex.ToString());
                }
                finally
                {
                    con.Close();
                }
            }

In generated CMD I have this query:

INSERT INTO sde."RegistruNote" (first_id, discipline, date, second_id, note, key) VALUES  
(131415, 'Painting', '4/20/2012 12:00:00 AM', 432431, 'asd', 'aWbhxZn3X'), 
(131415, 'Painting', '4/20/2012 12:00:00 AM', 432432, 'fas', '3Dl8YCSgX'), 
(131415, 'Painting', '4/20/2012 12:00:00 AM', 432433, 'asd', '8RiBgcjuL'), 
(131415, 'Painting', '4/20/2012 12:00:00 AM', 432434, 'fds', '8rAW1VfC7'), 
(131415, 'Painting', '4/20/2012 12:00:00 AM', 432435, 'gfd', 'LhRP2y5Ji'), 
(131415, 'Painting', '4/20/2012 12:00:00 AM', 432436, 'wer', 'lGVa5boYs'), 
(131415, 'Painting', '4/20/2012 12:00:00 AM', 432437, 'wer', 'hbFfo2zNg'), 
(131415, 'Painting', '4/20/2012 12:00:00 AM', 432438, 'we', 'xEq7iAhMi'), 
(131415, 'Painting', '4/20/2012 12:00:00 AM', 432439, 'rew', 'drFNV9sCm'), 
(131415, 'Painting', '4/20/2012 12:00:00 AM', 432410, 'tw', 'dJ5XwJLQy');

In catch I have this exception: PgSqlExcetion date/time field value out of range: "4/20/2012 12:00:00 AM"

Where do I have to convert the date to dd.mm.yyyy format ?

看一下使用PgSqlParameter对象,而不是将整个查询构建为字符串-可能是服务器未设置为识别客户端正在使用的语言环境的情况。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM