dbf file operations in C# .net - c#

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?

Related

How to convert a CSV into a SQLite database using C#

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#?

saving/reading csv file from/to database in C#

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

How to save a csv file into a database?

I am making a window application in asp.net using C#. I want to browse and import .csv file and save it in the database. Importing part I have done. Now, how am i supposed to save it in the database ? I am new to the language so please help
You can use ADO.NET, you will need to look up Data Adapters and the like.
If your Importing the Spreadsheet into a DataTable or DataSet you can use a Data Adapter to populate a table in the Database using an INSERT command.
Look up http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.aspx
Hope This Helps.
Once you have a file upload mechanism in place, (for example, using the FileUpload control), just iterate through the lines in the CSV file on the server, parse them into fields using an existing CSV reader (don't try to code your own unless your data is completely trivial, you won't handle all of the edge cases), and just execute a bunch of SQL INSERT statements against your database using your calling convention of choice.

C# reading an Excel work book, OLEDB or Excel Interop?

I need to read an Excel work book with 2 sheet which have more than 60,000 records. The application is an ASP.Net application so the performance matters.
Which approach should I take? Should I do it using Microsoft.Office.Interop.Excel or should I do it using OLEDB in ADO.Net?
What is the best approach?
Split the book into two worksheet files, convert them to CSV (comma-separated value) form, and process them as text.
I think that would be the best approach perfomance-wise, if you only need to read the data and do not care about formatting, merged cells, formulae etc.
I used always OLEDB in ADO.Net for it.
What the best aproach is, I don't know. But the easiest is like Kaerber says, to read it as 2 .csv files.
Then you can read it like you read a .txt file without 'fancy' libraries.
Use OLEDB for querying (reading) and Interop when modifying Excel.
You can consider using:
gemboxsoftware.com/spreadsheet
or the free alternative:
http://npoi.codeplex.com/

Import/Export CSV from SQLite from C# code

I am trying to figure out an eazy way to load CSV file into SQLite DB using System.Data.SQLite
I saw commandline way to do that i.e .Import mydata.csv mytable
But I need to do this via C# code. Any Idea?
you can create OleDbConnection to CSV file (just google it, it is very easy) then load rows to DataSet, then put that dataset into Sqlite by SqliteConnection. Few lines of code.
You could use the file helpers library if you want also.And well use the ado.net functionality too.

Categories

Resources