0

I need to import data from Excel to Sql Server using ASP.NET. How can I do this?

2
  • 2
    Does it need to be done through ASP.net? If it's a once-off import, you could use the DTS wizard.
    – mopoke
    Commented Dec 28, 2009 at 8:42
  • Through Asp.net code.I need the proper code.
    – Nandini
    Commented Dec 28, 2009 at 8:47

4 Answers 4

2

You can use ADO.net OLEDB data source. You can fetch records as you normally do for MS Access. Have a look at the example..

public static DataTable SelectAll()
{
    string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + @"\YourExcellfile.xls;Extended Properties=""Excel 8.0;HDR=Yes"";";
    OleDbConnection oleConnection = new OleDbConnection(conString);

    OleDbCommand oleCommand = new OleDbCommand("select * from [YourSheet1$]", oleConnection);
    OleDbDataAdapter adapter = new OleDbDataAdapter(oleCommand);

    oleConnection.Open();

    DataTable dt = new DataTable();
    adapter.Fill(dt);

    oleConnection.Close();

    return dt;
}

After the import you can pick the data from the data table and perform the insert operation using ADO.net Sql operation

1
  • FYI, last I heard, the Jet drivers were still only supported on x86, with no plans for x64.
    – RickNZ
    Commented Dec 28, 2009 at 13:37
0

I assume you want your users to upload an Excel document, which then has to be imported into SQL server. If so, you can either try some third-party library to open xls file and read data on a row-by-row basis, inserting it into an appropriate table or install Excel itself on a web server (not a good idea, though) and use it as an ODBC data source.

1
  • i want this to be happen through asp.net code.I need the proper code for this
    – Nandini
    Commented Dec 28, 2009 at 8:46
0

Besides using an ODBC data source, you can also to ask your user to export that Excel file to CSV and to import it manually.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.