Im a beginner, I need to access excell sheet using windows C# .NET, it will be g8 if you fix it else can you get me a better code?
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Data.OleDb;
namespace Project01 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Project1.xls;Extended Properties=""Excel 8.0;HDR=YES;"""; using (OleDbConnection connection = new OleDbConnection(excelConnectionString)) { OleDbCommand command = new OleDbCommand("Select ID,Data FROM [Data$]", connection); connection.Open(); // Create DbDataReader to Data Worksheet using (DbDataReader dr = command.ExecuteReader()) { // SQL Server Connection String string sqlConnectionString = "Data Source=.;Initial Catalog=Test;Integrated Security=True"; // Bulk Copy to SQL Server using (SqlBulkCopy bulkCopy =new SqlBulkCopy(sqlConnectionString)) { bulkCopy.DestinationTableName = "ExcelData"; bulkCopy.WriteToServer(dr); } } } } } }
the error i get: The type or namespace name 'DbDataReader' could not be found (are you missing a using directive or an assembly reference?)
The best overloaded method match for 'System.Data.SqlClient.SqlBulkCopy.WriteToServer(System.Data.IDataReader)' has some invalid arguments
Argument '1': cannot convert from 'DbDataReader' to 'System.Data.IDataReader'
|
| Author: ABitSmart 27 Jun 2009 | Member Level: Diamond | Rating:  Points: 2 |
command.ExecuteReader will return a OleDbDataReader.
using (DbDataReader dr = command.ExecuteReader())
should be,
using (OleDbDataReader dr = command.ExecuteReader())
|
| Author: Deepika Haridas 27 Jun 2009 | Member Level: Diamond | Rating:  Points: 2 |
Hi,
Check my resource
http://www.dotnetspider.com/resources/24568-Create-Excel-Application.aspx
Thanks & Regards, Deepika Editor
If U want to shine like a SUN..First U have to burn like the SUN!! Need a Guide? Join my mentor program..
|
| Author: ABitSmart 27 Jun 2009 | Member Level: Diamond | Rating:   Points: 3 |
DbDataReader is part of System.Data.Common namespace. Is that namespace included ?
|
| Author: Vinodramkumar.K 27 Jun 2009 | Member Level: Bronze | Rating:  Points: 2 |
ABitSmart, actually i changed the DbDataReader into OleDbDataReader, But there is a new runtime error, "Could not find installable ISAM." what might be the problem?
|
| Author: ABitSmart 27 Jun 2009 | Member Level: Diamond | Rating:  Points: 2 |
Vinod, the error generally comes when you have a corrupted registry etc.
Take a look at an alternate method to read excel, http://www.codeproject.com/KB/office/fasterexcelaccesstoc.aspx
This method uses Office interops.
|