I want to register contents of CSV file as one string in VB

Asked 2 years ago, Updated 2 years ago, 86 views

When registering csv files in db in vb, I want to register them as one character (~,~,~,~).
I understand that you use string.join, but I don't know what to do with csv

Affected Codes

DimstrAry As String()=csv// Is this the wrong place?
Dims1AsString=String.Join(",",",strAry)

Thank you for your cooperation.

csv vb.net

2022-09-29 22:45

1 Answers

If you have a real file rather than a list or array of data created by some task, why don't you load it into one variable using the FileSystem.ReadAllText method?

With the .NET Core lineage, you will need to call the Encoding.RegisterProvider(EncodingProvider) method beforehand.

Imports System.Text
Imports Microsoft.VisualBasic.FileIO

Encoding.RegisterProvider (CodePagesEncodingProvider.Instance)
Dims1AsString=FileSystem.ReadAllText("path/to/csvfile", Encoding.GetEncoding(932))

Alternatively, if the question says "Register the csv file to db" and you want the "whole file" to be a single string for each line, you can use the File.ReadLines method

if you want to be a single string for each line.

The data in the string must not contain a new line code, but if you do the following, the databody in the string array will contain data in each line.
If you specify it by index, you will be able to retrieve any row of data.
Adjust the presence or absence of headers as necessary.

Imports System.IO
Imports System.Text

Encoding.RegisterProvider (CodePagesEncodingProvider.Instance)
Dimcsvlines()As String=File.ReadAllLines("path/to/csvfile", Encoding.GetEncoding(932))
Dim headerlines As Integer=1
Dim database As Integer=csvlines.Length-headerlines
Dim database (dataccount-1) As String
Array.Copy (csvlines, headerlines, database, 0, database)

It's only been a day since I posted the question, but it doesn't seem to respond to comments from multi-post destinations, so I'll give you various ideas.

"The question says ""csv file,"" but if you're actually asking because you want to concatenate multiple strings instead of files, isn't it true that you'd rather know how to concatenate multiple strings into a list or array than a single string?"

There is one case in which the first line of the question source works correctly, where the variable csv is defined in a one-dimensional array of the same string as strAry, which contains valid data.

DimstrAry As String()=csv// Is this the wrong place?

Otherwise, if you don't know how to declare string variables as arrays, this article will help.
Arrays in Visual Basic
[Introduction to VB.NET] Summary of how to use the array!A thorough explanation of everything from basics to applications
How do I declare an array in VB.NET?

For example, initialize with declaration:

DimstrAry As String() = {"ABC", "DEF", "GHI"}

For example, specify the number, declare, and substitute:

DimstrAry(2)As String
strAry(0) = "ABC"
strAry(1) = "DEF"
strAry(2) = "GHI"

For example, increase (or decrease):

ReDim Preserve strAry(5)

Still, if it is a csv file, it is rarely a one-dimensional array of strings (=one-line file only), but it is usually a two-dimensional array (multiple lines).
In addition to registering with db, if you want to leverage the read csv data, it will be an array or a list of some class objects.
In such cases, it would be easy and fast to use the libraries described below.

As an introduction, it's for C#, but since it's .NET, it can be used on VB.B.
】Summary CThree recommended CSV libraries for use in C#
Top 20 NuGetcsv Packages
[C#] Introducing "CsvHelper" to read and write CSV

If you want to do VisualBasic, you can also make it using TextFieldParser, which is also used in C#.
[VB.NET] Load CSV files super easily using TextFieldParser!
[C#][VB.NET] How do I load a CSV (TSV) file in TextFieldParser
Read CSV files using VB.NET TextFieldParser
VB.NET-CSV file read operation

Either way, the csv file must be clear about how much data it contains and how it can be registered with db and used on the program.


2022-09-29 22:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.