Click here to Skip to main content
15,399,050 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
    string strConnection = ConfigurationManager.ConnectionStrings
["EmployeeConnectionString1"]"ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Create connection string to Excel work book
        string excelConnectionString =
        @"Provider=Microsoft.Jet.OLEDB.4.0;
        Data Source=C:\Book1.xlsx;
        Extended Properties=""Excel 8.0;HDR=YES;""";
        
        //Create Connection to Excel work book
        OleDbConnection excelConnection =
        new OleDbConnection(excelConnectionString);
        
        //Create OleDbCommand to fetch data from Excel
        OleDbCommand cmd = new OleDbCommand
        ("Select [Deptno],[DeptName],[DeptLoc] from [Book1$]  ", excelConnection);
        if(excelConnection.State==ConnectionState.Closed)
        excelConnection.Open();
        OleDbDataReader dReader;
        dReader = cmd.ExecuteReader();
        
        SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
        sqlBulk.DestinationTableName = "Dept";
        
        sqlBulk.WriteToServer(dReader);
    }
}

Error i am getting---OleDbException was unhandeled by user,cannot update database or object is read-only
Posted
Updated 6-Dec-11 12:40pm
v3
Comments
Wendelius 6-Dec-11 6:39am
   
What's the error and at what line you get the error?
developerit 7-Dec-11 5:32am
   
I have converted Book1.xlsx to Book1.xls through Excel. and use the same code it still not working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900