We help IT Professionals succeed at work.
Get Started

How can i get column name from excel to create sql column?

458 Views
Last Modified: 2013-12-17
i need to help to generate column name from excell automatically. i think that: we can do below codes:
CREATE TABLE [dbo].[Addresses_Temp] ( 
    [FirstName]   VARCHAR(20), 
    [LastName]    VARCHAR(20), 
    [Address]     VARCHAR(50), 
    [City]        VARCHAR(30), 
    [State]       VARCHAR(2), 
    [ZIP]         VARCHAR(10) 
) 

Open in new window


via C#. How can i learn column name from excel?
  private void Form1_Load(object sender, EventArgs e)
        {
            ExcelToSql();
        }


        void ExcelToSql()
        {

            string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Source\MPD.xlsm;Extended Properties=""Excel 12.0;HDR=YES;""";
            // if you don't want to show the header row (first row)            
            // use 'HDR=NO' in the string             
            string strSQL = "SELECT * FROM [Sheet1$]";            
            OleDbConnection excelConnection = new OleDbConnection(connectionString);           
            excelConnection.Open(); // This code will open excel file.            
            OleDbCommand dbCommand = new OleDbCommand(strSQL, excelConnection);          
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand);            
            // create data table            
            DataTable dTable = new DataTable();            
            dataAdapter.Fill(dTable);            
            // bind the datasource          
          //  dataBingingSrc.DataSource = dTable;         
            // assign the dataBindingSrc to the DataGridView         
           // dgvExcelList.DataSource = dataBingingSrc;             // dispose used objects          
            if (dTable.Rows.Count > 0)
                MessageBox.Show("Count:"+dTable.Rows.Count.ToString());
            dTable.Dispose();           
            dataAdapter.Dispose();           
            dbCommand.Dispose();            
            excelConnection.Close();           
            excelConnection.Dispose();
        }

Open in new window

Comment
Watch Question
Top Expert 2010
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE