Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Import Data from Excel

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 451
    Comment on it

    Import Data from Excel in C#

    Use the code below to import data from excel

    1. OleDbConnection cnn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=""E:\Book1.xlsx""; Extended Properties=Excel 12.0;");
    2. OleDbCommand oconn = new OleDbCommand("select * from [sheet1$]", cnn);
    3. cnn.Open();
    4. OleDbDataAdapter adp = new OleDbDataAdapter(oconn);
    5. DataTable dt = new DataTable();
    6. adp.Fill(dt);
    7. try
    8. {
    9. SqlConnection cnn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ToString());
    10. SqlCommand cmd = new SqlCommand();
    11. cnn1.Open();
    12. for (int i = 0; i < dt.Rows.Count; i++)
    13. {
    14. cmd = new SqlCommand("Insert into Student ([Name],[FatherName])" +
    15. "Values ('" + dt.Rows[i]["name"] + "','" + dt.Rows[i]["fname"] + "'")", cnn1);
    16. cmd.ExecuteNonQuery();
    17. // Modify your command as per your need.
    18. }
    19. cnn.Close();
    20. }
    21. catch (Exception ex)
    22. {
    23. }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: