开发者

importing of data from Excel To Access database by c#.net

I am working on a windows application in c#.net where I need to import an excel sheet into an Access database by the use of code in c#. I found the following code on the net and tried to use :

 string path = @"D:\project_excel"; 
      OleDbConnection con;
        System.Data.DataTable dt = null;
        //Connection string for oledb
        string conn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + path + "; Extended Properties=Excel 8.0;";
        con = new OleDbConnection(conn);
        try
        {
            con.Open();

            dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            String[] excelsheets = new String[dt.Rows.Count];
            int i = 0;

            foreach (DataRow dr in dt.Rows)
            {
                excelsheets[i] = dr["TABLE_NAME"].ToString();
                i++;
            }
            // here i manaually give the sheet number in the string array
            DataSet ds = new DataSet();
            foreach (string temp in excelsheets)
            {
                // Query to get the data for the excel sheet 
                //temp is the sheet name
                string query = "select * from [" + temp + "]";
                OleDbDataAdapter开发者_开发技巧 adp = new OleDbDataAdapter(query, con);
                adp.Fill(ds, temp);//fill the excel sheet data into a dataset ds
            }

        }
        catch (Exception ex)
        {

        }
        finally
        {
            con.Close();
        }

However it gives an exception which is mentioned below :

The Microsoft Jet database engine cannot open the file 'D:\project_excel'. It is already opened exclusively by another user, or you need permission to view its data.

Further I don't know the meaning of Extended Properties. I am using Microsoft Office 2007 package.If I set Extended Properties = 7.0 it gives following error :

Could not find installable ISAM.

Please help by providing some code sample.

Thanks in advance..


Check to see if the connection string is correct for your Access database version.

For details Visit ConnectionString.com

by

Mohamed Inshafudeen J.


For the error : Could not find installable ISAM.

1: save your excel file as "Excel 97-2003 wrokbook " 2: and also include the reference "Microsoft.Office.Interop.Excel"

best of luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜