Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How to export csv to db in C#. It works fine when you have a plain text simple text. But in my case I need to parse headers as single row.
There are many options!
use some driver custom functions (depends on your database)
use a [schema.ini](
https://learn.microsoft.com/en-us/sql/odbc/microsoft/schema-ini-file-text-file-driver)
do it manually ...
You can do it manually with while iteration. Before using loop, you should read csv file from path with StreamReader.
For example:
using(var csvReader = new StreamReader(System.IO.File.OpenRead("your file path")))
{
while (!csvReader.EndOfStream)
{
var rows = csvReader.ReadLine();
var columns = rows.Split(';');
if(values.Length > 1)
{
// Your logic getting values to save db
}
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last month.
Improve this question
Some time we need to import or insert all the CSV files located in a directory into database. The following C# code will insert all the files in a directory into database: const string CSV_CONNECTIONSTRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="{0}";Extended Properties="text;HDR=YES;FMT=Delimited""
As we don't have a lot ot information, we just can give you some tips in order to achieve what you want:
You need to install a NuGet package that provides CSV reading functionality. One sample of it it's "CsvHelper library" CSV Library
Then just use it, you will in the docs how to use it, but one sample of it would be:
void Main()
{
using (var reader = new StreamReader("path\\to\\file.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = csv.GetRecords<dynamic>();
foreach (var record in records) {
// whatever u want
}
}
}
Hope that helps.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a big function that returns a list of objects. I basically need the results to be put in to a text file. The list contains objects of a class with attributes string Index and integer count. I would want it to be written in a textfile like:
Index : Count fe.
BOOK_FROM_STORE : 27
Anybody has some guidance?
Parse the data held by your object to string and then write it to a text file.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
IN SQL SERVER can create XML Document like following format with every column name come with Datafield FieldName="Batch"
CID,BATCH,EXP_DATE are column names.
enter image description here
Try something like
SELECT 'BATCH' AS [DataField/#fieldName]
,BATCH AS [DataField]
,''
,'EXP_DATE' AS [DataField/#fieldName]
,EXP_Date AS [DataField]
FROM SomeWhere
FOR XML PATH('Memory');
The empty string in the middle is needed to start a new element
And be aware, that the dateformat will be ISO8601 and not 190615 (you should be happy about this!)
If you want to achieve XML document creation from C#, then it can be achieved through ADO.net with Help of DataSet. Method name is DataSet.WriteXml(string Filename).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
This is a sample of the textdelimited file
"11- 4-2014","20:54:22","","3974","1","1","1"
"11- 4-2014","20:55:25","","1411","1","1","1"
"11- 4-2014","20:55:26","","3177","1","1","1"
"11- 4-2014","20:55:32","","4051","1","1","1"
I need it to parse and write to a text file looking like
ID DateTime Area Numb1 Num2 Num3
1 11/4/2014-20:47:48 4297 1 1 1
2 11/4/2014-20:52:03 4013 1 1 1
The validation part comes in to check if 'Area' actually has 4 numbers, if it doesn't, Write all errors to a separate text file in the same format.
Check out this answer:
https://stackoverflow.com/a/20523165/4875896
quote:
Add a reference to Microsoft.VisualBasic.dll (works fine in C#, don't mind the name)
using (TextFieldParser parser = new TextFieldParser(#"c:\temp\test.csv"))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
while (!parser.EndOfData)
{
//Process row
string[] fields = parser.ReadFields();
foreach (string field in fields)
{
//TODO: Validate field and save as needed.
}
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
When reading a text file by using StreamReader, how can I get information from the contents on that line
for example:
Line 1: Hi there, my name is bob
Line 2: 1234563 90312
Line 3: I jumped over the moon
How would I detect 90312 on line 2? or anything else I may want on other lines?
Thanks.
You can check whether the current line contains some specific string. Such as:
string line;
using (StreamReader reader = new StreamReader("file.txt"))
{
line = reader.ReadLine();
if(line.Contains("90312"))
{
// Do something fancy here.
}
}
As far as I understand your question, you probably want to read a line into a variable and then tokenize it based on word-to-word basis.
You can use > stringObject.Split(' '); where, stringObject contains the line in question.