 |
plc.myforum.ro PLCs users www.plc.myforum.ro!
|
| View previous topic :: View next topic |
| Author |
Message |
btsp Junior

Joined: 10 Mar 2019 Posts: 1
|
Posted: 10/03/2019 18:15 Post subject: B&R PLC - File I/O - Reading data from a .csv file |
|
|
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 |
|
 |
wunci rattone Junior

Joined: 12 Sep 2018 Posts: 7
|
Posted: 11/03/2019 22:02 Post subject: |
|
|
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 |
|
 |
|
|
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
|