C#excel upload Batch Import SQL Server

Source: Internet
Author: User

Front desk:

<x:fileupload id="Btnupfield"runat="Server"Label="upload Excel Bulk Import user Information"Showlabel="true"> </x:FileUpload> <x:button id="Button1"text="Upload"runat="Server"icon="systemsavenew"OnClick="Btnupfield_click"> </x:Button>

Background:

 protected voidBtnupfield_click (Objectsender, EventArgs e) {            //upload files that need to be imported to the server            stringFilePath =""; stringFileextname =""; stringmyFileName;//You can't use it, but write it.            stringMyPath; stringFullName ="";//Save the file's full filename            if(BtnUpField.PostedFile.FileName! ="")            {                //Get file pathFilePath =BtnUpField.PostedFile.FileName; //get file name extensionFileextname = filepath.substring (Filepath.lastindexof (".") +1); //to determine whether an Excel file                if(Fileextname = ="xls"|| Fileextname = ="xlsx")                {                    Try                    {                        //gets the physical path corresponding to the virtual path specified on the Web serverMyPath = Server.MapPath ("~/upfile/"); //Get file namemyFileName = filepath.substring (Filepath.lastindexof (".") +1); //get the current time and name it "always and everywhere" to avoid duplication                        stringStrdatename = DateTime.Now.ToString ("HHMMSS"); FullName= MyPath + Strdatename +"."+Fileextname;                    BtnUpField.PostedFile.SaveAs (FullName); }                    Catch(Exception ex) {Response.Write (ex).                    Message); }                }                Else{alert.show ("file format is incorrect", Messageboxicon.error); return; }            }            //read the contents of Excel//string strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + FullName + "; Extended properties= ' Excel 8.0; hdr=yes;imex=1; ' "; /excel2003//string strconn = String.Empty;            stringstrconn ="Provider=microsoft.ace.oledb.12.0;data source="+ FullName +"; Extended properties= ' Excel 12.0; Hdr=yes; Imex=1 '";//excel2007 above OleDbConnection Connxls=NewOleDbConnection (strconn); if(Connxls. state.tostring () = ="Closed") {Connxls.            Open (); }            stringSqlexcel ="SELECT * FROM [sheet1$]"; OleDbDataAdapter Myda=NewOleDbDataAdapter (Sqlexcel, Connxls); //DataTable m_tablename=new DataTable ();DataSet MyDs =NewDataSet (); //m_tablename = Connxls.            GetOleDbSchemaTable (OleDbSchemaGuid.Tables, NULL); //if (m_tablename! = null && m_tableName.Rows.Count > 0)//{            //m_tablename.tablename = m_tablename.rows[0]["table_name"].            ToString (); //}            Try{Myda.fill (myDs,"[sheet1$]"); }            Catch(Exception ex) {Response.Write ("data format is wrong!"+Ex.            Message); }            if(myds.tables[0]. Rows.Count >0)//Data Binding            {                inti =0; Try                {                                    }                Catch(Exception ex) {Response.Write ("Read Error"+Ex.                Message); }                 for(i =0; I < myds.tables[0]. Rows.Count; i++) {Txtloginname.text= myds.tables[0]. rows[i][0].                    ToString (); txtName.Text= myds.tables[0]. rows[i][1].                    ToString (); txtPassword.Text= myds.tables[0]. rows[i][2].                    ToString (); Txtafreshpwd.text= myds.tables[0]. rows[i][3].                    ToString (); if(myds.tables[0]. rows[i][4]. ToString (). Equals ("male") ) {RbSex.SelectedItem.Value="male"; } Else{RbSex.SelectedItem.Value="female"; }                Save (); } alert.show ("congratulations on your saving success"+"Total"+ myds.tables[0]. Rows.Count +"Bar data,"+"is saving section"+ i +"Strip", MessageBoxIcon.Information);        } pagecontext.registerstartupscript (Activewindow.gethiderefreshreference ()); }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.