[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# excel to SQL

Posted on 2012-08-22
2
Medium Priority
?
782 Views
Last Modified: 2012-08-22
FYI: this is C#.asp code:

I used this code before to export data from excel to SQL but there were some issues. Some of the cells where not excported successfully to the table.

protectedvoid btnSend_Click(object sender, EventArgs e)

{

 String strConnection = "Data Source=MySystem;Initial Catalog=MySamplesDB;Integrated Security=True";

//file upload path

string path = fileuploadExcel.PostedFile.FileName;

//Create connection string to Excel work book

string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";

//Create Connection to Excel work book

OleDbConnection excelConnection =new OleDbConnection(excelConnectionString);

//Create OleDbCommand to fetch data from Excel

OleDbCommand cmd = new OleDbCommand("Select [ID],[Name],[Designation] from [Sheet1$]",excelConnection);

excelConnection.Open();

OleDbDataReader dReader;

dReader = cmd.ExecuteReader();

SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);

//Give your Destination table name

sqlBulk.DestinationTableName = "Excel_table";

sqlBulk.WriteToServer(dReader);

excelConnection.Close();

}

}

Open in new window


So, I thought maybe I need to choose differant types for my sql table. This is my columns what types should I create for them in my table:

PSID,      Payroll Status      ,Clock/Employee Number      ,Last Name      ,First Name      ,Department ID      ,Reason for vacation,      Hours of Vacation Left,            Department Name

ALSO, I need this C# code to Delete the previous table data and import the new excel sheet into the table when its selected.
0
Comment
Question by:pauledwardian
2 Comments
 
LVL 12

Accepted Solution

by:
jmcmunn earned 1500 total points
ID: 38323369
without seeing the data...impossible to say.  But here would be "typical" data types:

PSID - uniqueidentifier or integer/bigint
Payroll Status - varchar or integer
Clock/Employee Number - varchar (most likely)
Last name - varchar
first name - varchar
department ID - varchar (most likely) or possibly integer/bigint or guid depending on data
reason - varchar (or possibly text or vachar(max)
hours of vacation left - decimal with appropriate precision (or float)
department name - varchar


As far as deleting the data...a simple Delete from [Table] would handle it, I imagine.
0
 

Author Closing Comment

by:pauledwardian
ID: 38323462
Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question