C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Training   ASP.NET Web Hosting    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

Play Silverlight Games or Submit your Silverlight applications and earn 90% AdSense revenue.

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Resources » Code Snippets » ASP.NET WebForms »

Upload excel file content to SQl server using SQL bulk copy method


Posted Date: 08 Jan 2009    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: AbhiMember Level: Gold    
Rating: Points: 10



This code shows how to upload excel file content to SQL Server


protected void btnImport_Click(object sender, EventArgs e)
{
lblError.Visible = false;

string xlsFile = fileImport.FileName; //fileImport is the file upload control

if (xlsFile == "" || xlsFile.EndsWith(".xls") != true)
{
lblError.Text = "Please select an Excel File";

lblError.Visible = true;

return;
}

string targetFileName = MapPath("~/_OrgData/Excel/" + xlsFile);

fileImport.PostedFile.SaveAs(targetFileName);

string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + targetFileName + ";Extended Properties='Excel 4.0;HDR=YES;'";

OleDbConnection xlsConn = new OleDbConnection(excelConnectionString);

xlsConn.Open();

try
{
OleDbCommand command = new OleDbCommand("Select * FROM [Sheet1$] WHERE Username <> NULL", xlsConn);

DbDataReader dr = command.ExecuteReader();

string sqlConnectionString = TAData.Common.GetConnectionString();

using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "tblPeopleTemp";

bulkCopy.ColumnMappings.Add("Username", "UserName");
bulkCopy.ColumnMappings.Add("Password", "Password");
bulkCopy.ColumnMappings.Add("Surname", "LName");
bulkCopy.ColumnMappings.Add("First Name", "FName");

bulkCopy.WriteToServer(dr);
}
}

catch (Exception ex)
{
lblError.Text = "The Excel content is not valid";

lblError.Visible = true;
}

xlsConn.Close();

File.Delete(targetFileName);
}





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Upload excel file to SQl server - bulk copy  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Merge two GridView Header column in ASP.NET
Previous Resource: Set Configuration Settings for a particular folder/page in Web.config
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use