Advertisement

11.15.2007 at 09:56AM PST, ID: 22963601
[x]
Attachment Details

import excel to sql

Asked by KS_Mis in Visual Studio .NET 2005, Microsoft Visual C#.Net

Tags: excel, import, sql

I am trying to insert records into an existing sql table from an excel file in c# asp.net.  I would like to only insert specifi columns (both from the excel table and to the sql table) and specific rows (from the excel file).  All the help I could find was
insert into <sql> select * from <excel>

The error I receive is error in the INSERT INTO statement, but no details on what is failing.  See attached code.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
if (FileUpload1.FileName == "")
                return;
 
            FileUpload1.SaveAs("c:\\EmployeeExpenses.xls");
 
            SqlConnection SQLConn = new SqlConnection("Data Source=KSSQL01;Initial Catalog=stamp;Integrated Security=SSPI");
            SQLConn.Open();
            SqlCommand SQLCommand = new SqlCommand("Delete tempEmployeeExpenses", SQLConn);
            SQLCommand.ExecuteNonQuery();
            SQLConn.Close();
 
            OleDbConnection ExcelConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\EmployeeExpenses.xls;Extended Properties=Excel 8.0;");
            ExcelConnection.Open();
            OleDbCommand ExcelCommand = new OleDbCommand(
                "INSERT INTO [ODBC;Driver={SQL Server};Server=KSSQL01;Database=stamp;Trusted_ Connection=yes].[tempEmployeeExpenses] "+
                "(Date, Description, CardMember, Amount) "+
                "SELECT [Date], [Description], [CardMember], [Amount] "+
                "FROM [Sheet1$] "+
                "WHERE [Sheet1$].[CardMember] = \"" + CardHolderName + "\";", ExcelConnection);
                
            ExcelCommand.ExecuteNonQuery();
            ExcelConnection.Close();
[+][-]11.15.2007 at 10:14AM PST, ID: 20291036

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Visual Studio .NET 2005, Microsoft Visual C#.Net
Tags: excel, import, sql
Sign Up Now!
Solution Provided By: vbturbo
Participating Experts: 2
Solution Grade: A
 
 
[+][-]11.15.2007 at 10:15AM PST, ID: 20291047

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.15.2007 at 11:39AM PST, ID: 20291710

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11.15.2007 at 11:53AM PST, ID: 20291811

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.15.2007 at 05:26PM PST, ID: 20294864

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628