Advertisement

03.10.2008 at 07:53AM PDT, ID: 23228550
[x]
Attachment Details

Import excel sheet on web client using sqlbulkcopy

Asked by helena80 in Web Languages/Standards, .NET, Programming for ASP.NET

Tags: ,

I'm using sqlbulkcopy to get data from an excel sheet and save it to  a table en sqlserver2005. My code works fine when the excel file is in my aplication server, but it doesn't work when it is in the web client. I use an input type="file" to get the excel's file path from the client's machine but sqlbulcopy searchs the file by default in the aplication server.
Is there a way to get the file directly from the client machine? If it is not possible with sqlbulkcopy I would really apretiate other suggestions to do it.
 
This is the code I'm using:
 try
            {
               
                string path = fileExcel.Value;
               
                string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=" + '"' + "Excel 8.0;HDR=YES;" + '"';                                
                using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
                {
                    OleDbCommand command = new OleDbCommand("Select * FROM [Hoja1$]", connection);
                    connection.Open();
                using (DbDataReader dr = command.ExecuteReader())
                    {
                      string sqlConnectionString = "Data Source=ipserver; Initial Catalog=bd;user id=user; password=password";

                      using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
                        {
                       bulkCopy.DestinationTableName = "NombreTabla";
                           
                            try
                            {
                                bulkCopy.WriteToServer(dr);
                   
                            }
                            catch (Exception ex)
                            {
                                lblError.Text = ex.ToString();    
                            }
                            finally
                            {
                                dr.Close();
                               
                            }                          
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.ToString();          
            }          
Start Free Trial
 
 
[+][-]03.11.2008 at 06:15PM PDT, ID: 21102197

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

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

 
[+][-]03.12.2008 at 05:49AM PDT, ID: 21105316

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: Web Languages/Standards, .NET, Programming for ASP.NET
Tags: C#, Internet Explorer
Sign Up Now!
Solution Provided By: TheLearnedOne
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628