Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
If anyone have code for excel database connectivity in c# in website then pls help me..
Posted
Updated 29-Sep-13 20:24pm
v2
Comments
[no name] 30-Sep-13 2:56am    
excel database..? u mean ms access ..?
Member 10305815 30-Sep-13 2:59am    
excel spreadsheet..
Bernhard Hiller 30-Sep-13 3:11am    
Do you really have to use an Excel spreadsheet as a database? That's a very very very bad idea... Better take Microsoft SQL Server.
And: Excel has some "compatibility" issues with ASP.
Member 10305815 30-Sep-13 3:17am    
thank u for ur suggestion.

Given below is a working code

C#
If fileExtension = ".xls" Then
     ExcelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fileName & ";Extended Properties='Excel 8.0;HDR=YES;'"
 Else
     ExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & fileName & ";Extended Properties='Excel 12.0 Xml;HDR=YES;'"
 End If


 Dim dtFill As New DataTable

 Using oleDBConn As New OleDbConnection(ExcelConn)
     Dim daFill As New OleDbDataAdapter("Select * from [Attendance$]", ExcelConn)
     daFill.Fill(dtFill)
 End Using
 
Share this answer
 
v2
Make use of OLEDB connection.
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";


or plz check the belwo code

public static DataTable ReadExcel(string path)
{
    //create a DataTable to hold the query results
    DataTable dTable = new DataTable();

    try
    {
        if (!File.Exists(path))
            return null;

        //create the "database" connection string 
        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";

        //create the database query
        string query = "SELECT * FROM [Sheet1$]" ;

        //create an OleDbDataAdapter to execute the query
        OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
        //fill the DataTable
        dAdapter.Fill(dTable);
        dAdapter.Dispose();
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }

    return dTable;
}
 
Share this answer
 

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