943,989 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Marked Solved
  • Views: 2139
  • MS SQL RSS
Dec 19th, 2008
0

Copy data from excel to ms sql

Expand Post »
Hi, I am trying to copy som data from an excel document that I have on the server into a table in my ms sql server.
I think the problem now is that I am having trouble connecting to the sql server.

I'm using sql server 2005.

You can see the error message here:
http://www.excel.web.surftown.dk/default.aspx
and then click on the button.

This is all the code I have written:
MS SQL Syntax (Toggle Plain Text)
  1. USING System;
  2. USING System.Collections.Generic;
  3. USING System.Linq;
  4. USING System.Web;
  5. USING System.Web.UI;
  6. USING System.Web.UI.WebControls;
  7. USING System.DATA.SqlClient;
  8. USING System.DATA;
  9. USING System.Configuration;
  10. USING System.Collections;
  11. USING System.Web.Security;
  12. USING System.Web.UI.WebControls.WebParts;
  13. USING System.Web.UI.HtmlControls;
  14. USING System.DATA.Common;
  15. USING System.DATA.OleDb;
  16.  
  17.  
  18.  
  19.  
  20. public partial class _Default : System.Web.UI.Page
  21. {
  22. protected void Page_Load(object sender, EventArgs e)
  23. {
  24.  
  25. }
  26.  
  27.  
  28. protected void Button1_Click(object sender, EventArgs e)
  29. {
  30. string excelConnectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;""", Server.MapPath("database.xls"));
  31.  
  32.  
  33.  
  34. USING (OleDbConnection connection =
  35. new OleDbConnection(excelConnectionString))
  36. {
  37.  
  38. OleDbCommand command = new OleDbCommand
  39. ("Select ID,Data FROM [Sheet1$]", connection);
  40.  
  41. connection.Open();
  42.  
  43. USING (DbDataReader dr = command.ExecuteReader())
  44. {
  45. string sqlConnectionString = "Data Source=;Initial Catalog=Test;Integrated Security=True";
  46.  
  47. USING (SqlBulkCopy bulkCopy =
  48. new SqlBulkCopy(sqlConnectionString))
  49. {
  50. bulkCopy.DestinationTableName = "ExcelData";
  51. bulkCopy.WriteToServer(dr);
  52.  
  53.  
  54. }
  55. }
  56. }
  57. }
  58. }

I hope someone can help me with the problem?

Kischi
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kischi is offline Offline
62 posts
since Dec 2008
Dec 19th, 2008
0

Re: Copy data from excel to ms sql

I actually got it to work now.
I just had to change the datasource.

Thanks anyway. :-D

Kischi
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kischi is offline Offline
62 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: query for selecting last column from sqldb
Next Thread in MS SQL Forum Timeline: Fulltext Index problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC