471,073 Members | 1,156 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

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
Jul 23 '07 #1
2 2213
tifoso
41
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
Jul 23 '07 #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
Here's what I tried:
// Connection String to Excel Workbook
string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Documents and Settings\saharris\My Documents\first_last_name.xls;Extended Properties=""Excel 8.0;HDR=YES;""";

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

connection.Open();

// Create DbDataReader to Data Worksheet
using (OleDbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=myIpaddress;Initial Catalog=master;User=myUserName;Password=myPassword ";

// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "GUEST";
bulkCopy.WriteToServer(dr);
}
}
}
}
Jul 23 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by R. Santiago | last post: by
3 posts views Thread by Art at ABE Computer Consultants | last post: by
2 posts views Thread by pontifikas | last post: by
2 posts views Thread by Meaney | last post: by
9 posts views Thread by dba123 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.