Can you please suggest how can we export the oracle clob data field to csv file. The table contain other regular data types too. And also if the clob data cannot fit in a csv what are the other options available?
Thanks, Naveen
You can read clob's just like strings. new System.Data.OracleClient.OracleConnection() and so on. When you have the data, you can write CSV's with a StreamWriter because they are just text files.
Storing CLOBs in a CSV file is possible if you use a serious CSV writer that supports:
Multi-line fields
Doubling of double quotes
Setting the encoding
Then you can read the exported file in any other application that has a serious CSV parser, such as Excel.
Just don't go for any CSV classes that have the word simple in their name. They certainly will be too simple.
Related
I'm struggling to figure out how to convert a CSV file into a database. I've tried a few methods here but I can't wrap my head around it. I have a CSV file with thousands of rows and I need to convert that into a
SQLite database using C#. Any help is appreciated!
You can leverage MS Excel, open the CSV file and specify your character separator as needed (I believe it will default to tab limited). You can now save your thousands of rows as an Excel spreadsheet versus the character separated file (CSV) format.
Then you can leverage the open source OpenXML libraries to open the Excel document and work with it using object model. In an object oriented fashion, you can programatically create your new database using SQL statements.
Query for the spreadsheet headers to be used as your column names. Of course you'll need to ensure that your source CSV had provided appropriate headers. These can easily be added to the top of the large file if not.
E.g.
https://learn.microsoft.com/en-us/office/open-xml/how-to-get-a-column-heading-in-a-spreadsheet
Next, you simply iterate the rows and construct your SQL statement to insert the rows. You can review the Open XML docs, Microsoft docs, or existing StackOverflow docs for sample code on how this is easily done.
How to read xslx with open XML SDK based on columns in each row in C#?
I am trying to fetch data from database and export it to .csv file format, which is similar to ETL process. But I want to do this in C#.
SQL Query to fetch data from database.
Format the data to a specified file specification.
Convert it to .csv file.
I know that 1st step is easy to do, I am struggling to find a way for 2nd and 3rd step.
I can't comment on the file specification, as you haven't described it, but writing CSV files is pretty easy. Unlike XML etc the format is so simplistic you can write directly to a StreamWriter using WriteLine. All you need is to output a first line that contains the column names separated with commas, then for each row returned from your SQL Query write the column values in the same order separated by commas. The only real gotcha is escaping, e.g. dealing with commas, quotes, etc by surrounding each value with quotes and escaping any quotes in the value.
The example below does just that for a DataTable:
http://dotnetguts.blogspot.co.nz/2007/01/exporting-datatable-to-csv-file-format.html
I figured out a simple and efficient way to do this:
1.SQL Query to fetch data from database.
2. Format the data to a specified file specification.
For the first 2 steps, I have used the Dapper.NET, which
took care of the database part as well as formatting. Dapper helped to convert the SQL results to a LIST by which I fulfilled the file specifications.
3.Convert it to .csv file.
For converting the SQL results to CSV file, I have used FileHelpers
Library which is more simple than I expected.
Hope it will help someone.
Is it possible to save a csv file to a SQL Server Database table using C#?
Do need to convert the csv file to Binary format in order to do this?
Is there any other way, like saving it in text format(csv is a basically a txt file)?
I'd appreciate it if you could provide some example code.
CSV files are just text. You can create a table that has an nvarchar(max) column and store your CSV files in there.
You have another helpful C# sample for import and export of CSV file with SQL Server. Below is the link:
http://www.codeproject.com/Articles/30705/C-CSV-Import-Export
You can use the data import wizard I believe and point it at a csv file. This should create a table that matches the structure of your imported csv
I need to create a dbf file which has the table structure of an existing dbf and insert data to it. Also I need to specify the NAME for it which i usually do in excel by insert->Name.
Which is the best and easy way for it. I have tried using OLEDB already. But I need suggestions for the best option.
Have you tried:
System.IO.File.Copy(sourceFileName, destFileName);
Followed by opening the new file and truncating the data leaving the table structure?
Hey guys, I need to export data from database which contains Unicode(like Japanese characters).
While when i trying to export to csv files, it just becomes the garbage....
First, I need to read data from database and save it to an datatable
Second, I will make a query based on the datatable (Using Linq)
Last, Export the queried results to csv file
Is there any way to solve this problem?
Thanks a lot.
Internally .Net (C#) always uses unicode. The problems occur when you export the data. Then you need to specify an encoding.
If you use File.CreateText you immediatly create a UTF-8 encoded file.
Does the application you use for import, recognize the UTF-8 encoding, or does it need a specific encoding?
Try to set connection data collation to unicode_ci or more specidfied.