I want to upload an excel file through a windows form application in C # and want to import data into a database (Mysql server). How can I do it??? I created a form that requires me to upload an excel file to the mysql database. its voluminous insert data into the database table.
My Excel file Contains columns such as userid, password, first_name, last_name, user_group AND MySql database table (aster_users) Contains many columns such as userid, password, first_name, last_name, user_group, queue, active, created_date, created_by, role ..
I need to upload this excel file to my database, and other columns will become empty or null, which is not an issue.
My shape design 
Here is my C # code:
using MySql.Data.MySqlClient; using System; using System.Data.SqlClient; using System.Windows.Forms; namespace UploadFileToDatabase { public partial class Form1 : Form { public Form1() { InitializeComponent(); } String MyConString = "SERVER=******;" + "DATABASE=dbs;" + "UID=root;" + "PASSWORD=pwsd;" + "Convert Zero Datetime = True"; private void BtnSelectFile_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "Text files | *.csv"; if (dlg.ShowDialog() == DialogResult.OK) { string fileName; fileName = dlg.FileName; txtfilepath.Text = fileName; } } private void btnUpload_Click(object sender, EventArgs e) { string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtfileparth.Text + ";Extended Properties=\"Excel 12.0;HDR=YES;\""; using (OleDbConnection connection = new OleDbConnection(connectionString)) { OleDbCommand command = new OleDbCommand ("Select * FROM [Sheet1$]", connection); connection.Open(); using (DbDataReader dr = command.ExecuteReader()) { string sqlConnectionString = MyConString; using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString)) { bulkCopy.ColumnMappings.Add("[userid]", "userid"); bulkCopy.ColumnMappings.Add("password", "password"); bulkCopy.ColumnMappings.Add("first_name", "first_name"); bulkCopy.ColumnMappings.Add("last_name", "last_name"); bulkCopy.ColumnMappings.Add("user_group", "user_group"); bulkCopy.DestinationTableName = "aster_users"; bulkCopy.WriteToServer(dr); MessageBox.Show("Upload Successfull!"); } } } }
This is how I tried .i got an error message like this
Additional information: the external table is not in the expected format.
in this line connection.Open (); . How can I do that?
Arthi source share