0

我想在数据库中插入数据表

    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();
                }
            }

在生成的 CMD 中,我有这个查询:

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');

在捕获中我有这个异常: PgSqlExcetion 日期/时间字段值超出范围:“4/20/2012 12:00:00 AM”

我必须在哪里将日期转换为 dd.mm.yyyy 格式?

4

1 回答 1

1

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

于 2012-04-20T08:00:10.203 回答