Inserting excel column into specific sql table column

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lildiapaz
    New Member
    • Jul 2007
    • 38

    Inserting excel column into specific sql table column

    Can someone please explain to me how to select a specific column in an excel spreadsheet and import the information in that column to an sql table in a c# windows application. Let's say my excel spreadsheet is full of first and last names and dates. What I want to do is take column "A" and insert the names in that column into an sql table with the column name "first_name ". And I want to do the same thing for the last name column

    Example
    Let's say my excel spreadsheet has the following
    Excel
    Column A Column B Column C
    jack johnson 4/2/2007
    tim jackson 3/6/1995
    ricky thomas 1/2/2090
    bobby moore 4/2/1997

    I want to take Column A and insert in the SQL table Person in the column firstname

    Can you please help me with some code.
    this is very important,

    any help will be appreciated
  • tifoso
    New Member
    • Apr 2007
    • 41

    #2
    If u try and show some code more help will come your way

    Open an object(Excel type) , browse to the sheet/columns you are interested
    and insert into your table

    Ciao

    Comment

    • lildiapaz
      New Member
      • Jul 2007
      • 38

      #3
      Originally posted by tifoso
      If u try and show some code more help will come your way

      Open an object(Excel type) , browse to the sheet/columns you are interested
      and insert into your table

      Ciao
      Here's what I tried:
      // Connection String to Excel Workbook
      string excelConnection String = @"Provider=Micr osoft.Jet.OLEDB .4.0;Data Source=D:\Docum ents and Settings\saharr is\My Documents\first _last_name.xls; Extended Properties=""Ex cel 8.0;HDR=YES;""" ;

      // Create Connection to Excel Workbook
      using (OleDbConnectio n connection = new OleDbConnection (excelConnectio nString))
      {
      OleDbCommand command = new OleDbCommand("S elect firstname FROM [first_last_name $]", connection);

      connection.Open ();

      // Create DbDataReader to Data Worksheet
      using (OleDbDataReade r dr = command.Execute Reader())
      {
      // SQL Server Connection String
      string sqlConnectionSt ring = "Data Source=myIpaddr ess;Initial Catalog=master; User=myUserName ;Password=myPas sword ";

      // Bulk Copy to SQL Server
      using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sql ConnectionStrin g))
      {
      bulkCopy.Destin ationTableName = "GUEST";
      bulkCopy.WriteT oServer(dr);
      }
      }
      }
      }

      Comment

      Working...