Guest User

Guest User

  • Tech Writer
  • 77
  • 14.1k

problem with insert data into postgresql database

Jul 28 2020 9:36 PM
code below have a problem to insert data into PostgreSQL database
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Remoting.Channels;
using System.Data.SqlClient;
using EO.Internal;
using Npgsql;

namespace TryingMyBest
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

        }

        private void tmrLoop_Tick(object sender, EventArgs e)
        {
            tmrLoop.Enabled = false;

            string dirName = @"C:\ipub\ftp";

            DirectoryInfo di = new DirectoryInfo(@"C:\ipub\ftp");
            FileInfo[] size = di.GetFiles();

            DirectoryInfo info = new DirectoryInfo(@"C:\ipub\ftp");
            FileInfo[] filess = info.GetFiles().OrderByDescending(p => p.LastWriteTime).ToArray();

            var connString = "Host=localhost;Port=**;Username=*******;Password=*********;Database=**********";
            var conn = new NpgsqlConnection(connString);
            conn.Open();

            try
            {
                var files = Directory.EnumerateFiles(dirName);

                foreach (string currFile in files)
                {
                    Console.WriteLine();
                }
                foreach (FileInfo file in filess)
                {
                    DateTime lastWriteTime = file.LastWriteTime;
                    MessageBox.Show("Time last Edit : " + lastWriteTime);
                }
                foreach (string path in Directory.EnumerateFiles(@"C:\ipub\ftp", "*.csv"))
                {
                    string[] lines = System.IO.File.ReadAllLines(path);
                    foreach (string line in lines)
                    {
                        Console.WriteLine(line);
                        string[] columns = line.Split(',');
                        foreach (string column in columns)
                        {
                           
                        }
                    }
                }
                foreach( var NpgsqlConnection in connString)
                {
                    IDbCommand command = conn.CreateCommand();
                    string sql = "INSERT INTO result (Time, engineNo, operator, lotNo, rpm, temperature, oilPressure) VALUES (@Time,@engineNo,@operator,@lotNo,@rpm,@temperature,@oilPressure)";
                    command.CommandText = sql;
                    command.ExecuteNonQuery();
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            tmrLoop.Enabled = true;
        }

        private void btnOnStart_Click(object sender, EventArgs e)
        {
            tmrLoop.Enabled = true;
        }

        private void btnOnStop_Click(object sender, EventArgs e)
        {
            tmrLoop.Enabled = false;
        }
    }
}
 
it will pop up error 42703 : column 'time' of relation 'result' does not exist
 
help me, thank you
im using visual studio 2019

Answers (3)