Click here to Skip to main content
15,922,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to import the data from excel sheet to sql server database in asp net using c#
Posted

This is how Google [^]replies to your query.

Please try some examples yourself. If you face any difficulty while coding, feel free to come back here and ask another question with specific issue describing the scenario.
 
Share this answer
 
CREATE PROC [Ins_Test]
AS
INSERT INTO TABLE_NAME
SELECT * FROM OPENDATASOURCE('MICROSOFT.ACE.OLEDB,12.0'
,'Data Source=c://Q.xlsx;Extended Properties="Excel 12.0"'
)...[Sheet1$]

-------------Call this Proc in .cs page---------------
 
Share this answer
 
Refer:
Import Data from Excel to SQL Server[^]
This may help.
 
Share this answer
 
Hi UdhayaKumar,

Here is the code

public void ExportToSql(string filename,string savepath)
        {
            
            string connectionString = "";
            savepath = savepath + "\\" + filename;
            if (System.IO.Path.GetExtension(filename) == ".xls")
            {
                connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + savepath + ";Extended Properties=Excel 8.0";
            }
            else if (System.IO.Path.GetExtension(filename) == ".xlsx")
            {
                connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +savepath  + "; Extended Properties=Excel 12.0;";
            }

            using (OleDbConnection con = new OleDbConnection(connectionString ))
            {
                
                con.Open();

                OleDbCommand com = new OleDbCommand("Select * from [Sheet1$]", con);

                OleDbDataReader dr = com.ExecuteReader();
                
                using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["OnlineExamConnectionString"].ConnectionString))
                {

                    sqlcon.Open();

                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlcon))
                    {

                        //bulkCopy.ColumnMappings.Add("[SrNo]", "CollID");
                       
                        //bulkCopy.ColumnMappings.Add("[College Name]", "Name");

                        bulkCopy.DestinationTableName = "tblCollege";

                        bulkCopy.WriteToServer(dr);

                    }

                }

                dr.Close();

                dr.Dispose();
            }


        }


Reference :
[^]

http://www.c-sharpcorner.com/UploadFile/99bb20/import-excel-data-to-sql-server-in-Asp-Net/[^]

If you are fine with the solution ,please rate the solution provided :)

Regards,
Praveen Nelge
 
Share this answer
 
v2

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