Import csv file content to SQL Server using C# - c#

I have an Excel file on my desktop, and I would like to use C# to insert Excel content to SQL Server, how sould I do? (use Entity Framework and console to complete it)
I use windows application(.NET Framework) and use code first to build my model.
Now I had build the table in SQL Server but I'm not sure how to read Excel file from desktop and insert data in excel file to SQL Server.
I just find Excel file from desktop then I don't know what should I do for next step.
string path = #"\Users\chun0\Downloads\filename.csv";
string input = File.ReadAllText(path);
Could anyone help me?
Thank you all so much.

This is, step by step, explanation of doing just what you ask, if I got it correct. https://www.c-sharpcorner.com/article/import-excel-data-to-database-using-Asp-Net-mvc-entity-frame/

Related

How to update data in excel File using C#?

This Question might be repeated, But I couldn't get solution regarding my problem so far. I'm new to Interop. I'm using excel file (as a database).
Here is data presentation in excel file
in my data If Card ID repeated then I need to increment '1' in Counter in the same row, similarly I need to fetch IP address of same row..
I'm using Interop Excel approach to insert data in excel file..
Kindly tell me how can I perform that update operation to that excel file through C# (WPF)
Sorry for bad English..
Thanks
I recommend using Closed XML
You write to the file directly and don't need Excel. It will need to be the latest version of an Excel file to work (The open xml standard).
Epplus.dll or npoi.dll will also read/write to excel files w/o excel.
Save the data in an XML or JSON file, then when you want to visualize them you create the excel file from these data, so you will have a very light file and easy to read and update if you wish.
I haven't done this specifically through wpf, but you can access powershell cmdlets through .net and powershell has commands for retrieving and writing Excel data.
That said, my experience has been it's very tedious and inconsistent with bugs. I would tell your client that using an Excel file as a database is impossible and certainly prone to failure in practice.
For one thing you will run into read/write restrictions if it is used by anything else.
If you don't mind to use comercial libraries, you can try to use Aspose.Cells. It has rich cells API and able to work without Excel interop API.

c# sending data from excel to SQL database

I want to be able to send data from an excel file to an SQL database.
The Excel file only has one column and will always be this way.
I am currently working on a project where people will place excel files into a folder which will need to be read and put into sql, I am able to monitor the folder currently and am able to capture the file name of that file to a variable which i can call to select the excel file.
What i cannot find is a simple excel to sql tutorial that doesnt rely on the import wizard.
If you are able to help please do and if you want any further information please feel free to ask
Thanks alot :)

Export SQL server data into a CSV file

I've just taken care of a project that requires converting data from the old website (built in C# web form) to the new one (using PHP).
In the old database, there is a product table contains 1600 products that I need to export into a CSV file then import to the new database but I have no idea to do that.
I can access the current server but I don't see any .cs file (code-behind file). I also have no idea how to export that table to a CSV file using myLittleTools (The SQL server management tool that Plesk provide).
Can any have me please?
Thank you very much.
Try SQL Server Import and Export Wizard. This is a tool provided by MSSQL .
Reference links here:
https://learn.microsoft.com/en-us/sql/integration-services/import-export-data/import-and-export-data-with-the-sql-server-import-and-export-wizard
https://learn.microsoft.com/en-us/sql/integration-services/import-export-data/get-started-with-this-simple-example-of-the-import-and-export-wizard
You can export to either or excel or directly map two tables of different databases and transfer data
According to the MyLittleAdmin site you can opent the table to view the data and export it form there.

Importing 2 .txt files to sql server database using asp.net

I have 2 .txt files with data that i need to import to a sql server database in order to continue my project in Visual Studio C#. I was told to use the Stream writer/reader. Can someone explain to me how to use it, and show me all the aspects of how to do it? I am very new.
If you just need to insert the information by hand, you can follow the tips provided here.
If you want to do it through C#, here are some links to get you started:
First you need to parse the text files to retrieve the data. Here's an example on how to do that.
Next you'll need to insert the information. Here's a Beginners guide to doing that.
Good luck!
I have an example here.
http://granadacoder.wordpress.com/2009/01/27/bulk-insert-example-using-an-idatareader-to-strong-dataset-to-sql-server-xml/
It was written for VS2003, but updating it to VS2010 or (or 2008 or other) would be trivial.
It does not use a stream reader. It uses OleDb.
The above is a good solution if you need to do any validations on the data before inserting it.
Here is another idea.
http://www.codeproject.com/Articles/27802/Using-OleDb-to-Import-Text-Files-tab-CSV-custom

Programmatically convert SQLite to .sql

Alright, this will probably sound a bit silly. The more I think about it, the sillier it sounds.
Background:
The company I'm working for hired a guy about a year ago to write some software for them. This software (written in VB6) is for a drilling supplies manufacturer (sensors, bits, etc). Anyway, the software is installed at a rig site, and provides a plethora of information (bit depth, rpms, etc). Now, a reading is taken every 10 seconds, and saved to a file. (I was initially told a local SQL database, but that's proved incorrect.) This file, which can apparently only be opened in SQLite Database Browser (opening it in any text editor results in looking at garbage, essentially).
Move Forward:
I was hired to write a program that takes the data that is saved, and create a daily report based upon it. Initially, I was told that there was a MySQL database on the local machine that is storing the information. However, I just recently found out that that is not the case. It's stored in this file. I can open the file in SQLite Database Browser, and export it as a .sql file, and that is all awesome and such. Problem is, my employer wants this done programmatically.
My Question:
Is there a way to convert this SQLite file to .sql programmatically, or what is the best route to get this information uploaded into a local MySQL database? Thank you for much for any assistance.
Have you considered just using a [the] SQLite data provider for .NET and opening and reading the data out that way? You could easily read the data out of the SQLite database, and the write it into the MySQL database (using the appropriate MySQL data provider).
Here's the SQLite data provider I've used in the past: http://sqlite.phxsoftware.com/ I'm not sure if there's something newer (or better).
Use the SQLite Manager application you have to make an *.sql file dump of the data in the SQLite database. Import the resulting *.sql file into a MySQL database.
Write a C# script that will use this code:
sqlite_db_handler.query('SELECT * FROM dbname.sqlite_master WHERE type='table'');
To get a list of all the tables in the SQLite database.
Iterate over each of these tables and use this code:
sqlite_db_handler.query('SELECT * FROM '+table_name);
Iterate over each of the resulting rows using this code:
mysql_db_handler.query('REPLACE INTO '+table_name+' VALUES('+row.join("','")+')');
Done!
P.S.: It's pseudo-code I'm not a C# developer.

Categories