using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using MySql.Data.MySqlClient; //Add Mysql Library
using System.Configuration;



namespace MindLab
{
    public partial class ImportFile : Form
    {
        MySqlConnection myConnect;
        MySqlCommand cmd;
        string strSQL = null;

        public ImportFile()
        {
            InitializeComponent();
            //InitializeOpenFileDialog();
        }







        private void button3_Click(object sender, EventArgs e)
        {
            SetOrderProgram so = new SetOrderProgram();
            so.Show();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {

            DialogResult dr = this.openFileDialog1.ShowDialog();
            FileImportText.Text = openFileDialog1.FileName;

            if (dr == System.Windows.Forms.DialogResult.OK)
            {

                string connectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", openFileDialog1.FileName);
                string query = String.Format("select * from [{0}$]", "Sheet1");
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
                DataSet dataSet = new DataSet();
                dataAdapter.Fill(dataSet);
                dataGridView1.DataSource = dataSet.Tables[0];

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                MySqlConnection connection = new MySqlConnection();
                MySqlCommand cmd = new MySqlCommand();
                string strSQL = null;

                ConnectionStringSettings conSetting = ConfigurationManager.ConnectionStrings["zybersoftwork"];
                string connectionString = conSetting.ConnectionString;
                myConnect = new MySqlConnection(connectionString);


                cmd.CommandText = @"INSERT INTO customer VALUES (order, 
                                                          prefix, 
                                                          cus_firstname, 
                                                          cus_lastname, 
                                                          born_date)";


                //cmd.Parameters.Add("@Order", MySqlDbType.VarChar).Value = 

                for (int i = 0; i <= dataGridView1.RowCount -1; i++)
                {


                }   
            }
            catch (Exception ex){
                MessageBox.Show(ex.ToString());

               }
        }

        private void ImportFile_Load(object sender, EventArgs e)
        {

        }
    }
}

i am beginner pls help me.
How to import excel to datagrid then save to mysql database c#

hi you can expose the structure of your excel ?

my excel have 5 colomn {hospital_number, firstname, lastname, sex, birthdate}

Hi, here is my code

//Load Excel
Workbook workbook = new Workbook(); 
workbook.LoadFromFile(@"..\..\parts.xls",ExcelVersion.Version97to2003);
Worksheet sheet = workbook.Worksheets[0];
//Export to DataGridView
DataTable dataTable = sheet.ExportDataTable();
this.dataGridView1.DataSource = dataTable;

I use a .NET Excel component

I need to generate an exam module i.e that picks marks of students from their cats, assignment and main exam and then adds them up and gives grading. please point me to the right direction as i start on it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.