Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8105687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:13:41+00:00 2026-06-06T00:13:41+00:00

I am importing an excel sheet into my sql database. Everything works fine, but

  • 0

I am importing an excel sheet into my sql database. Everything works fine, but I would like to read the column header in as static value. Not sure how to do this. Here is the code I am using to read the sheet in:

    'Connection String to Excel Workbook   
    Dim excelConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & MyFile & ";Extended Properties=""Excel 12.0;HDR=Yes;"""

    ' Create Connection to Excel Workbook   
    Using connection As New System.Data.OleDb.OleDbConnection(excelConnectionString)

        'List columns you need from the Excel file   
        Dim command As New System.Data.OleDb.OleDbCommand("Select * FROM [" & txtSheetName.Text & "$]", connection)
        connection.Open()

        ' Create DbDataReader to Data Worksheet   
        Using dr As System.Data.OleDb.OleDbDataReader = command.ExecuteReader()

            Dim strSql As String = ""

            strSql = "INSERT INTO tblMine (" & _
                    "Header1, Field1, Field2 " & _
                     ") VALUES (" & _
                    "Header1, Field1, Field2)"

            Try
                If dr.HasRows() Then

                    While dr.Read()

                        If Convert.ToString(dr.Item(1)) <> "" Then

                            Dim cmd As New SqlClient.SqlCommand
                            cmd.Connection = cn
                            cmd.CommandType = CommandType.Text
                            cmd.CommandText = strSql
                            'DataTable1.Columns[0].ColumnName 
                            cmd.Parameters.Add("@Header1", SqlDbType.VarChar).Value = ???
                            cmd.Parameters.Add("@Field1", SqlDbType.VarChar).Value = Convert.ToString(dr.Item(0))
                            cmd.Parameters.Add("@Field2", SqlDbType.Int).Value = Convert.ToInt32(dr.Item(1))

                            cmd.ExecuteScalar()

                        End If

                    End While

                End If
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:13:41+00:00Added an answer on June 6, 2026 at 12:13 am

    Not a very clean code, but this gives the Header1 (or the name in first row, first column of your sheet named [" & txtSheetName.Text & "$]"

    Dim cmd2 As OleDbCommand = New OleDbCommand("SELECT * FROM [" & txtSheetName.Text & "$] where 1=0", connection)
    Dim da As OleDbDataAdapter = new OleDbDataAdapter(cmd2)
    Dim dt As DataTable = new DataTable()
    da.Fill(dt)
    string header1 = dt.Columns(0).ColumnName
    

    A little optimization

         Dim cmd As New SqlClient.SqlCommand
         cmd.Connection = cn
         cmd.CommandType = CommandType.Text
         cmd.CommandText = strSql
         cmd.Parameters.AddWithValue("@Header1", header1) ' this is invariant
         cmd.Parameters.AddWithValue("@Field1", string.Empty) ' this is a dummy value
         cmd.Parameters.AddWithValue("@Field2", 0)  ' this is a dummy value
         While dr.Read()
             If Convert.ToString(dr.Item(1)) <> "" Then
                  cmd.Parameters("@Field1").Value = Convert.ToString(dr.Item(0))
                  cmd.Parameters("@Field2").Value = Convert.ToInt32(dr.Item(1))
                  cmd.ExecuteScalar()
             End If
        End While
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm importing the contents of an Excel 2007 file into a MySQL database but
I am importing Excel sheet into DataGridView.I have one of the cell has date
I have some questions about importing data from Excel/CSV File into SQL Server. Let
I've written the following function for importing excel files into matlab. The function works
I'm importing data in a SQL Server 2008 database from excel file where the
my requirement is to read all rows of an excel-sheet in first column that
I'm importing an Excel file to SQL through ASP.NET and a certain column includes
i am preparing myself for importing data from excel sheet to sql server 2005.I
i am currently working on importing data from excel sheet to sql server.i have
My VB.NET application is using importing some excel sheet into access command.CommandText = SELECT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.