Click here to Skip to main content
15,887,214 members
Home / Discussions / C#
   

C#

 
AnswerRe: Looking for simple graph library for C# windows console app Pin
jsundownr14-Nov-14 5:19
jsundownr14-Nov-14 5:19 
AnswerRe: Looking for simple graph library for C# windows console app Pin
BillWoodruff14-Nov-14 5:55
professionalBillWoodruff14-Nov-14 5:55 
GeneralRe: Looking for simple graph library for C# windows console app Pin
jsundownr14-Nov-14 6:09
jsundownr14-Nov-14 6:09 
GeneralRe: Looking for simple graph library for C# windows console app Pin
BillWoodruff14-Nov-14 7:12
professionalBillWoodruff14-Nov-14 7:12 
GeneralRe: Looking for simple graph library for C# windows console app Pin
jsundownr14-Nov-14 7:32
jsundownr14-Nov-14 7:32 
AnswerRe: Looking for simple graph library for C# windows console app Pin
Gerry Schmitz14-Nov-14 10:28
mveGerry Schmitz14-Nov-14 10:28 
GeneralRe: Looking for simple graph library for C# windows console app Pin
jsundownr14-Nov-14 13:52
jsundownr14-Nov-14 13:52 
QuestionExport data from Excel (xls file) to Sql data table by clicking on export button (c#) Pin
Member 1113495413-Nov-14 23:28
Member 1113495413-Nov-14 23:28 
Hi All,
I am using Visual Studio 2008 to design windows form Application.
I created one form, that will Browse and select excel file and by clicking on Export button, all data will transfer to sql database.

But while debugging, it stop at code
using (OleDbConnection connection = new OleDbConnection(excelConnectionString)) 


For your review i am giving full code below:
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.IO;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data.Common;


namespace CHD_CALL_REGISTER
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection();
       
        protected void Page_Load(object sender, EventArgs e)
        {
            string str = "Data Source=INLGM84MV1;Initial Catalog=CHD;Integrated Security=True";
            SqlConnection con = new SqlConnection(str);
            //con.ConnectionString = @"Data Source=INLGM84MV1;Initial Catalog=CHD;Integrated Security=True";
        }
        public Form1()
        {
            InitializeComponent();
        }
        
        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "C:\\";
            openFileDialog1.Title = "Browse Excel Files";

            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;

            openFileDialog1.DefaultExt = "xls";
            openFileDialog1.Filter = "Excel file (*.xls)|*.xls";
            //openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            openFileDialog1.ReadOnlyChecked = true;
            openFileDialog1.ShowReadOnly = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.SafeFileName;
                buttonexport.Enabled = true;
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                buttonexport.Enabled = false;
            }

        }

        private void buttonexport_Click(object sender, EventArgs e)
        {
            // Connection String to Excel Workbook,Replace DataSource value to point to your excel file location
            string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source" + textBox1.Text + ";Extended Properties\"Excel 8.0;HDR=YES\"";


            // Create Connection to Excel Workbook
            using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
            
            {
                OleDbCommand command = new OleDbCommand("Select * FROM [Sheet1$]", connection);

                connection.Open();

                // Create DbDataReader to Data Worksheet
                using (DbDataReader dr = command.ExecuteReader())
                {
                    // SQL Server Connection String
                    //string sqlConnectionString = "Data Source=localhost;Initial Catalog=MyDB;Integrated Security=True";

                    // Bulk Copy to SQL Server
                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(con))
                    {
                        bulkCopy.DestinationTableName = "EMP_TABLE";
                        bulkCopy.ColumnMappings.Add("EMPCODE", "EMPCODE");
                        bulkCopy.ColumnMappings.Add("EMP_NAME", "EMP_NAME");
                        bulkCopy.ColumnMappings.Add("BUSINESS_CODE", "BUSINESS_CODE");
                        bulkCopy.ColumnMappings.Add("JOIN_DT", "JOIN_DT");
                        bulkCopy.ColumnMappings.Add("HQ_NAME", "HQ_NAME");
                        bulkCopy.ColumnMappings.Add("ZONE_ID", "ZONE_ID");
                        bulkCopy.ColumnMappings.Add("DESIG", "DESIG");
                        bulkCopy.ColumnMappings.Add("BAND", "BAND");
                        bulkCopy.ColumnMappings.Add("DEPT_NAME", "DEPT_NAME");
                        bulkCopy.ColumnMappings.Add("EMP_STATUS", "EMP_STATUS");
                        bulkCopy.ColumnMappings.Add("SUP1", "SUP1");
                        bulkCopy.ColumnMappings.Add("SUP1NAME", "SUP1NAME");
                        bulkCopy.ColumnMappings.Add("SUP1HQ", "SUP1HQ");
                        bulkCopy.ColumnMappings.Add("SUP2", "SUP2");
                        bulkCopy.ColumnMappings.Add("SUP2NAME", "SUP2NAME");
                        bulkCopy.ColumnMappings.Add("SUP2HQ", "SUP2HQ");
                        bulkCopy.ColumnMappings.Add("SUP3", "SUP3");
                        bulkCopy.ColumnMappings.Add("SUP3NAME", "SUP3NAME");
                        bulkCopy.ColumnMappings.Add("SUP3HQ", "SUP3HQ");
                        bulkCopy.WriteToServer(dr);
                        MessageBox.Show("Data Exported To Sql Server Successfully");
                    }
                }
            }

        }
    }
}



Please help me...thanks in advance...
Vishal

AnswerRe: Export data from Excel (xls file) to Sql data table by clicking on export button (c#) Pin
Eddy Vluggen14-Nov-14 0:25
professionalEddy Vluggen14-Nov-14 0:25 
GeneralRe: Export data from Excel (xls file) to Sql data table by clicking on export button (c#) Pin
Member 1113495417-Nov-14 23:11
Member 1113495417-Nov-14 23:11 
GeneralRe: Export data from Excel (xls file) to Sql data table by clicking on export button (c#) Pin
Pete O'Hanlon17-Nov-14 23:52
mvePete O'Hanlon17-Nov-14 23:52 
GeneralRe: Export data from Excel (xls file) to Sql data table by clicking on export button (c#) Pin
Eddy Vluggen18-Nov-14 0:32
professionalEddy Vluggen18-Nov-14 0:32 
GeneralRe: Export data from Excel (xls file) to Sql data table by clicking on export button (c#) Pin
Member 1113495418-Nov-14 21:54
Member 1113495418-Nov-14 21:54 
GeneralRe: Export data from Excel (xls file) to Sql data table by clicking on export button (c#) Pin
Eddy Vluggen19-Nov-14 2:58
professionalEddy Vluggen19-Nov-14 2:58 
GeneralRe: Export data from Excel (xls file) to Sql data table by clicking on export button (c#) Pin
Member 1113495419-Nov-14 23:30
Member 1113495419-Nov-14 23:30 
GeneralRe: Export data from Excel (xls file) to Sql data table by clicking on export button (c#) Pin
Eddy Vluggen20-Nov-14 5:11
professionalEddy Vluggen20-Nov-14 5:11 
GeneralRe: Export data from Excel (xls file) to Sql data table by clicking on export button (c#) Pin
Member 1113495421-Nov-14 22:28
Member 1113495421-Nov-14 22:28 
QuestionRe: Export data from Excel (xls file) to Sql data table by clicking on export button (c#) Pin
Eddy Vluggen22-Nov-14 3:57
professionalEddy Vluggen22-Nov-14 3:57 
GeneralRe: Export data from Excel (xls file) to Sql data table by clicking on export button (c#) Pin
PIEBALDconsult22-Nov-14 4:55
mvePIEBALDconsult22-Nov-14 4:55 
QuestionHow To Increase Width Excel Cell Size When I Export Excel File Pin
Nishant.Chauhan8013-Nov-14 22:11
Nishant.Chauhan8013-Nov-14 22:11 
AnswerRe: How To Increase Width Excel Cell Size When I Export Excel File Pin
Dave Kreskowiak14-Nov-14 2:15
mveDave Kreskowiak14-Nov-14 2:15 
QuestionHow to create Data recovery application using c#.net windows form Pin
Srikanth5913-Nov-14 18:27
Srikanth5913-Nov-14 18:27 
AnswerRe: How to create Data recovery application using c#.net windows form Pin
Dave Kreskowiak13-Nov-14 18:32
mveDave Kreskowiak13-Nov-14 18:32 
RantRe: How to create Data recovery application using c#.net windows form Pin
Richard Deeming14-Nov-14 2:15
mveRichard Deeming14-Nov-14 2:15 
QuestionPlug in for Gecko Browser Window to view a PDF? Pin
PDTUM13-Nov-14 7:54
PDTUM13-Nov-14 7:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.