By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,995 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,995 IT Pros & Developers. It's quick & easy.

Inserting excel column into specific sql table column

P: 38
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
Share this Question
Share on Google+
2 Replies


tifoso
P: 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

P: 38
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.