4
Answers

insert from csv file to database

Code below to open CSV file, how to store the data from csv file row by row into database mysql.
 
Please help me, im using visual studio 2019
 
sing 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;

namespace TryingMyBest
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

        }

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

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

            DirectoryInfo di = new DirectoryInfo(@"C:\pub\ftp");
            FileInfo[] size = di.GetFiles();
            MessageBox.Show("Directory ini ada file: ", di.Name);

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

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

                foreach (string currFile in files)
                {
                    MessageBox.Show(currFile);
                    MessageBox.Show("Size dia: " + currFile.Length.ToString());
                    Console.WriteLine();
                }
                foreach (FileInfo file in filess)
                {
                    DateTime lastWriteTime = file.LastWriteTime;
                    MessageBox.Show("Time last Edit : " + lastWriteTime);
                }
                foreach (string path in Directory.EnumerateFiles(@"C:\pub\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)
                        {
                
                        }
                    }
                }
            }

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

Answers (4)