Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Import Excel into Sql Database

Import Excel into Sql Database

Scheduled Pinned Locked Moved ASP.NET
databasecsharpsql-serversysadminhelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rosjac
    wrote on last edited by
    #1

    Hi!! Could somebody help me in converting an excel spreadsheet to an sql server database. I need some working around this.. i hv it working in vb but the C# doesnt seem to fetch the same code. ... Thanks in advance !!

    M G 2 Replies Last reply
    0
    • R rosjac

      Hi!! Could somebody help me in converting an excel spreadsheet to an sql server database. I need some working around this.. i hv it working in vb but the C# doesnt seem to fetch the same code. ... Thanks in advance !!

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, IMO, you can use the ADO.NET to read data from an excel sheet, then insert it into SQL db. You can see the sample code below on how to use the ADO.NET to query data from an excel file: http://support.microsoft.com/kb/306572/[^]

      R 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, IMO, you can use the ADO.NET to read data from an excel sheet, then insert it into SQL db. You can see the sample code below on how to use the ADO.NET to query data from an excel file: http://support.microsoft.com/kb/306572/[^]

        R Offline
        R Offline
        rosjac
        wrote on last edited by
        #3

        Well my problem with that particular eg is tat it uses range.. I want the user to simply click the browse button which will gv the loc of the sheet/workbook and then by simply clickin the import the stuff shd rest accurately into the sql db. lots of egs are seen in Vb but c#gives import to xml and then to db. Help ideas..pls!!

        1 Reply Last reply
        0
        • R rosjac

          Hi!! Could somebody help me in converting an excel spreadsheet to an sql server database. I need some working around this.. i hv it working in vb but the C# doesnt seem to fetch the same code. ... Thanks in advance !!

          G Offline
          G Offline
          Gowtham Sen
          wrote on last edited by
          #4

          Hi, try this. string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strfilename + ";Extended Properties=Excel 8.0;"; OleDbConnection mycon = new OleDbConnection(strConn); try { //Get the data from selected excel sheet. OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM ["+lstEmpSheets.SelectedValue+"]", strConn); if (mycon.State != ConnectionState.Open) //if connection is not open, open it mycon.Open(); //DataSet myDataSet = new DataSet(); myCommand.Fill(dt); mycon.Close(); } catch(Exception ex) { lblResult.Visible = true; lblResult.Text = ex.Message.ToString(); } finally { if (mycon.State == ConnectionState.Open) //if connection is not open, open it mycon.Close(); } try { if(dt!=null) { con = new SqlConnection((string) ConfigurationSettings.AppSettings["ConnectionString"]); con.Open(); cmd = new SqlCommand(); cmd.Connection=con; //Get the data from data table and update the database. foreach(DataRow dr in dt.Rows) { //Need to write relative code here. string empId = dr[0].ToString().Trim(); errRefEmp =empId; string name = dr[1].ToString().Trim(); string project = dr[2].ToString().Trim(); string supervisor= dr[3].ToString().Trim(); string mailId= dr[4].ToString().Trim(); if((empId.CompareTo("")==0)||(supervisor.CompareTo("")==0)|| string query = " INSERT INTO EMPLOYEE "; query += " VALUES ('"+empId+"','"+name+"','"+project+"','"+supervisor+"','"+mailId+"')"; cmd.CommandText=query; cmd.ExecuteNonQuery(); con.Close(); Page.RegisterStartupScript("SuccessfulEmpUpdation","alert('Excel file uploaded Successfully.!')"); Thanks and regards Gowtham sen

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups