C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » Windows »

Accessing a excel worksheet in c# windows .net


Posted Date: 27 Jun 2009      Posted By: Vinodramkumar.K      Member Level: Bronze     Points: 1   Responses: 5



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'







Responses

Author: ABitSmart    27 Jun 2009Member Level: DiamondRating: 2 out of 52 out of 5     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 2009Member Level: DiamondRating: 2 out of 52 out of 5     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 2009Member Level: DiamondRating: 3 out of 53 out of 53 out of 5     Points: 3

DbDataReader is part of System.Data.Common namespace. Is that namespace included ?


Author: Vinodramkumar.K    27 Jun 2009Member Level: BronzeRating: 2 out of 52 out of 5     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 2009Member Level: DiamondRating: 2 out of 52 out of 5     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.



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : Error
Previous : Import and export
Return to Discussion Forum
Post New Message
Category: Windows

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use