How to save a csv file into a database? - c#

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.

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

dbf file operations in C# .net

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?

Transferring .txt data into .sdf Using Visual C# to display

I'm using Visual c# express 2008.
I have a huge text file that has data similar to this: "text/text/text/text"
I'm using a delimiter to separate the data.
Now I want to transfer this data to an .sdf file that will be displayed in table format through the windowsformapplication using dataGridView.
I already created an sdf with a table (and columns).
I know how to access the .sdf and display through the dataGridView.
My problem is I have no idea how to transfer the data from the txt to the sdf.
I don't want to do it manually because the txt file contains around 20,000 worth of lines.
Resources: I've recycled code from
http://dotnetperls.com/datagridview-tutorial
Thanks to anyone who can help. :3 Ree
From memory (not tested) SqlBulkCopy works with SDF, so then all you need is an IDataReader. If you search for "fast CsvReader" you should find one on codeproject; this works with any delimiter and should work perfectly with SqlBulkCopy (I've used it this way multiple times, although not with sdf).
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx
http://www.codeproject.com/KB/database/CsvReader.aspx
Also, you could try my new Csv2sqlce utility: http://csv2sqlce.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.

LINQ : saving files to a database

I want to save a pdf and mp3 file(s) to a SQL Server database and be able to retrieve from it.
I'm still starting out with LINQ and don't master it yet.
You need to convert these into byte arrays (System.Data.Linq.Binary). One line to load
var myMp3 = new Binary(File.ReadAllBytes(mp3Filename));
If you create your database schema (VarBinary in the database) and drag the table over from Server Explorer into the DBML designer, it'll do everything for you.
To start off with your going to have a binary field in your database to save the file to.
Are you using LinqToSql, EntityToSql, or? Need some more information...
But once you get an object with a []byte to save the file to then it just a matter of making the appropriate Save() call... but with having some more information it hard to say.
Did you google tutorials?
Here is one that I found: Uploading Binary files or Images using LINQ to SQL
Has example code and sql to generate dummy tables...

Categories

Resources