-4

I'm really a newbie one.

I wanted to import data from excel or CSV file to MySQL using C#. I just need a button and then, viola! It should insert automatically to MySQL database.

I only know how the structure of my excel file and my MySQL database table looks like. Just don't know how to import it.

Please help. As always, thank you!

  • 1
    What research have you done so far? You just need to 2 google searches Read Excel with C# and MySQL Insert – Jeremy Thompson Oct 22 '16 at 05:49
  • 1
    we can not code it for you but there are lots of sources thanks to StackOverFlow. 1-) read from [Excel](http://stackoverflow.com/questions/15828/reading-excel-files-from-c-sharp) 2-) insert data to [MySql](http://stackoverflow.com/questions/13604216/how-i-can-insert-data-in-the-mysql-database) you could have done this searches before asking this question. – Badiparmagi Oct 22 '16 at 05:51
  • I already know how to read or open the excel file. I just don't know what command string to use and how it will be executed by c# after opening the file. – Eugene Ganancial Oct 22 '16 at 05:58
  • And I don't know what query to use. – Eugene Ganancial Oct 22 '16 at 06:02
  • @EugeneGanancial This site has plenty of answered questions about doing what you want. Try the search bar ;) – NuWin Oct 22 '16 at 06:10
  • @mmushtaq that's a horrible dupe ref imo – Drew Oct 22 '16 at 06:23
  • @Drew This question also contains a well explained answer as well as contains [Source Link](http://www.morgantechspace.com/2013/10/import-csv-file-into-sql-server-using.html) for more details – mmushtaq Oct 22 '16 at 06:28
  • @mmushtaq it is the wrong server type, plus it loops. So basically that reference is like a plane going down in flames your ref [here](http://stackoverflow.com/questions/20759302) ... which is wrong – Drew Oct 22 '16 at 06:29
  • and imo, the SO also trying to accomplish same thing which is mentioned in this question. – mmushtaq Oct 22 '16 at 06:29
  • It is the wrong server type, wrong technology stack, and even if it was right, it is 20 to 50 times slower – Drew Oct 22 '16 at 06:33
  • To make it simplier. Just wanted to do something like this, but in MySQL commands. The video is in MSSQL. https://www.youtube.com/watch?v=WF6y4FN-dg0 As always, thank you! – Eugene Ganancial Oct 22 '16 at 06:46

1 Answers1

0

This should be able to start you off.. but you could've found this if you actually searched Stack Exchange or Google. Also since you commented that you already know how to read an Excel file... here's a start to insert the data.

using MySql.Data.MySqlClient;

cont string connectInfo = "Server=yourServer;Database=yourDatabase;UID=yourUsername;Password=yourPassword";

var query = "insert into yourTable (column, column, column..) Values(@column1, @column2, @column3)";

using (var connect = new MySqlConnection(connectInfo)) {
 connect.Open();
 using (var cmd = new MySqlCommand(query, connect) {
   cmd.Parameters.AddwithValue("@column1", "something");
   cmd.Parameters.AddWithValue("@column2", "something");
   cmd.Parameters.AddWithValue("@column3", "something");
   cmd.ExecuteNonQuery();
 } 
}
NuWin
  • 276
  • 5
  • 15