plc.myforum.ro Forum Index plc.myforum.ro
PLCs users www.plc.myforum.ro!
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

B&R PLC - File I/O - Reading data from a .csv file

 
Post new topic   Reply to topic    plc.myforum.ro Forum Index -> Other PLCs
View previous topic :: View next topic  
Author Message
btsp
Junior
Junior


Joined: 10 Mar 2019
Posts: 1

PostPosted: 10/03/2019 18:15    Post subject: B&R PLC - File I/O - Reading data from a .csv file Reply with quote

I am currently trying to pull data from a .csv file.

a column or row i can format anyway to make it easier,

Currently i can read the data into a string, but i am stuck on how to separate that data from a string into an array of data points.

Using Structured Text

right now i am reading an entire row of data where the data points are separated by semi colons.

How can i point to each point of the string and pull data and store?

Thnak you for your help.

B&R structured text lib has only a few functions.

Thanks for some information/assistance
Back to top
View user's profile Send private message
wunci rattone
Junior
Junior


Joined: 12 Sep 2018
Posts: 7

PostPosted: 11/03/2019 22:02    Post subject: Reply with quote

https://stackoverflow.com/questions/13430218/how-to-read-from-csv-file-and-store-in-database-in-asp-net-using-c
https://stackoverflow.com/questions/41558125/reading-csv-file-trimming-filtering-the-data-then-storing-into-database-in-c-s
https://www.codeproject.com/Articles/11435/Importing-CSV-Data-and-saving-it-in-database

Open Visual Studio
Go to File ->New ->Project.
Then go to Visual C# ->Windows and select Console Application
Rename the project name as ReadCSVFile.
Right-click the Reference, click Add Reference,select Microsoft.VisualBasic, and click OK button

Code:
using System;

using System.Data;

using Microsoft.VisualBasic.FileIO;

namespace ReadDataFromCSVFile

  {

    static class Program

      {

        static void Main()

        {

            string csv_file_path=@"C:\Users\Administrator\Desktop\test.csv";

            DataTable csvData = GetDataTabletFromCSVFile(csv_file_path);

            Console.WriteLine("Rows count:" + csvData.Rows.Count);           

            Console.ReadLine();

        }

    private static DataTable GetDataTabletFromCSVFile(string csv_file_path)

        {

            DataTable csvData = new DataTable();

            try

            {

              using(TextFieldParser csvReader = new TextFieldParser(csv_file_path))

                 {

                    csvReader.SetDelimiters(new string[] { "," });

                    csvReader.HasFieldsEnclosedInQuotes = true;

                    string[] colFields = csvReader.ReadFields();

                    foreach (string column in colFields)

                    {

                        DataColumn datecolumn = new DataColumn(column);

                        datecolumn.AllowDBNull = true;

                        csvData.Columns.Add(datecolumn);

                    }

                    while (!csvReader.EndOfData)

                    {

                        string[] fieldData = csvReader.ReadFields();

                        //Making empty value as null

                        for (int i = 0; i < fieldData.Length; i++)

                        {

                            if (fieldData[i] == "")

                            {

                                fieldData[i] = null;

                            }

                        }

                        csvData.Rows.Add(fieldData);

                    }

                }

            }

            catch (Exception ex)

            {

            }

            return csvData;

        }

      }

    }
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    plc.myforum.ro Forum Index -> Other PLCs All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Fa-ti si tu propriul forum GRATUIT, pe www.myforum.ro