473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C# Import xls file into sql server

Hi can anybody tell what is error in following code, its running through .Net 2005 editor fine,
but when running on browser gives error:
No error message available, result code: E_FAIL(0x80004005).
Expand|Select|Wrap|Line Numbers
  1.  try
  2.         {
  3.             // Create Connection to Excel Workbook
  4.  
  5.             OleDbConnection connection = new OleDbConnection(excelConnectionString);
  6.  
  7.             if (connection.State == ConnectionState.Closed)
  8.             {
  9.  
  10.                 connection.Open();
  11.                      }
  12.  
  13.             OleDbCommand command = new OleDbCommand("select Name,Age,City,Phone FROM [Data$]", connection);
  14.  
  15.             // Create DbDataReader to Data Worksheet
  16.  
  17.             OleDbDataReader dr = command.ExecuteReader();
  18.  
  19.                      // SQL Server Connection String
  20.             string sqlConnectionString = "Data Source=localhost;database=demo;user id=sa;pwd=sa";
  21.  
  22.             // Bulk Copy to SQL Server
  23.  
  24.             SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlConnectionString);
  25.  
  26.                      bulkcopy.DestinationTableName = "ExcelData";
  27.  
  28.             //            bulkCopy.WriteToServer(dr);
  29.             bulkcopy.WriteToServer(dr);
  30.             bulkcopy.Close();
  31.             if (!dr.IsClosed)
  32.                 dr.Close();
  33.  
  34.    Response.Write("Data Imported From Xls file");
  35.             if (connection.State == ConnectionState.Open)
  36.                 connection.Close();
  37.         }
  38.  
  39.         catch (Exception ex)
  40.         {
  41.             Response.Write(ex.Message);
  42.         }
Dec 14 '07 #1
4 10898
Frinavale
9,735 Expert Mod 8TB
I think it might have something to do with your connection string.

See this article. It could really help you out.
Dec 14 '07 #2
I think it might have something to do with your connection string.

See this article. It could really help you out.
Thanks Frinavale,

It works,
I had not put the file in my application folder,
I had just put the file in my application folder and its worked

Thanks for help
Dec 15 '07 #3
I'm currently struggling to upload the Excel sheet data to the SQL Server using .Net. Please help me in provide the source code for it. I'll be very thnakful for the timely help.


Thanks Frinavale,

It works,
I had not put the file in my application folder,
I had just put the file in my application folder and its worked

Thanks for help
Oct 22 '08 #4
Frinavale
9,735 Expert Mod 8TB
I'm currently struggling to upload the Excel sheet data to the SQL Server using .Net. Please help me in provide the source code for it. I'll be very thnakful for the timely help.
This forum is dedicated to helping you understand your problems so that you can create your own solution. Please attempt to solve the problem before asking for help with it so that we can address any difficulties you face.

What have you tried so far?
What problems have you encountered?

Please post any code that you think might be causing the problem (remember to use code tags when posting code snippets)

-Frinny
Oct 22 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Benny | last post by:
Dear All, I am performing a data import on the SQL server. Due to fact that I use the excel file as a source. Some of cells in excel are actually empty, they become NULL fields after importing...
4
by: Kevin Forbes | last post by:
Hi there, When importing a text file using SQL Server, how do I specify the path to a file on the same remote server that SQL Server is running on? I tried //c:/filename but that doesn't seem to...
2
by: jet | last post by:
Hi, Maybe this is an easy task, but I'm having a really hard time figuring out how to do this. I'm a complete newbie to SQL Server. I have a database dump file from MySQL that's in .sql...
1
by: Chris | last post by:
Background: I am using a MS Access 2000 front end with SQL Server 8.0 back end. I have the requirement to import all text files (regardless of filename) from a given folder on the network into a...
1
by: Ralph Noble | last post by:
I have two avenues to access my primary SQL Server. (I work remotely using a VPN connection.) Usually, I hit the server from my local machine but I also login to a desktop machine inside the...
2
by: s.hong | last post by:
Hello? On the ASP.NET, I can use the class by import declation on the top of page. <%@ page language="vb" explicit="true" aspcompat="false" debug="true"%> <%@ import namespace="System.Data" %>...
1
by: David Berry | last post by:
Hi All. I'm looking for any help or sample code that can show me how to make a file import wizard in ASP.NET (VB preferred) like the one that MS Access uses. I'm working on a web site where the...
4
by: BostonNole | last post by:
I am looking for suggestions on the most efficient way to import 7 different fixed width files into a DataSet. Not all at the same time. One file at a time, but the format could change from file...
7
by: Randy | last post by:
Folks: We have a web-based app that's _really_ slowing down because multiple clients are writing their own private data into a single, central database. I guess the previous programmer did...
0
by: A3AN | last post by:
Hi. I receive a database backup on a daily basis. I then import this dump on another server which I use for software development. There is two db's being hosted on this server. We test software...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.