Copy data from excel to ms sql

Thread Solved
Reply

Join Date: Dec 2008
Posts: 37
Reputation: kischi is an unknown quantity at this point 
Solved Threads: 0
kischi kischi is offline Offline
Light Poster

Copy data from excel to ms sql

 
0
  #1
Dec 19th, 2008
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 37
Reputation: kischi is an unknown quantity at this point 
Solved Threads: 0
kischi kischi is offline Offline
Light Poster

Re: Copy data from excel to ms sql

 
0
  #2
Dec 19th, 2008
I actually got it to work now.
I just had to change the datasource.

Thanks anyway. :-D

Kischi
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC