Excel import Database in ASP.

Source: Internet
Author: User
Tags import database

protected voidButton1_Click (Objectsender, EventArgs e) {SqlConnection conn=NewSqlConnection (system.configuration.configurationmanager.connectionstrings["lyconnectionstring"]. ConnectionString);;//Link DatabaseConn.        Open (); Try        {            stringFileURL = TypeName (FileUpload1);//Call the TypeName method to get the Excel file pathDataSet ds =NewDataSet ();//Get Data SetDS =Xsldata (FileURL); intErrorcount =0;//record the number of error messages            intInsertcount =0;//record number of insert success bars            intUpdatecount =0;//record the number of update information bars             for(inti =0; I < DS. tables[0]. Rows.Count; i++)            {                stringStuid = ds. tables[0]. rows[i][0].                ToString (); stringStuname = ds. tables[0]. rows[i][1].                ToString (); stringStusex = ds. tables[0]. rows[i][2].                ToString (); stringZhuanye = ds. tables[0]. rows[i][3].                ToString (); stringClassName = ds. tables[0]. rows[i][4].                ToString ();                Response.Write (Stuid);                Response.Write (Stuname);                Response.Write (Stusex);                Response.Write (Zhuanye);                       Response.Write (classname); if(Stuid! =""&& Stuname! =""&& Stusex! =""&& Zhuanye! =""&& ClassName! ="") {SqlCommand selectcmd=NewSqlCommand ("Select COUNT (*) from stud", conn); intCount =Convert.ToInt32 (selectcmd.                    ExecuteScalar ()); if(Count >0) {SqlCommand selectcmd2=NewSqlCommand ("Select COUNT (*) from stud where stuid= '"+ Stuid +"'", conn); intCount2 =Convert.ToInt32 (SELECTCMD2.                        ExecuteScalar ()); if(Count2 >0) {SqlCommand updatecmd=NewSqlCommand ("Update stud set stuname= '"+ Stuname +"', stusex= '"+ Stusex +"', zhuanye= '"+ Zhuanye +"', classname= '"+ classname +"' where stuid= '"+ Stuid +"'", conn); Updatecmd.                            ExecuteNonQuery (); Updatecount++; }                        Else{SqlCommand Insertcmd=NewSqlCommand ("INSERT into stud values ('"+ Stuid +"', '"+ Stuname +"', '"+ Stusex +"', '"+ Zhuanye +"', '"+ classname +"')", conn); Insertcmd.                            ExecuteNonQuery (); Insertcount++; }                    }                    Else{SqlCommand Insertcmd=NewSqlCommand ("INSERT into stud values ('"+ Stuid +"', '"+ Stuname +"', '"+ Stusex +"', '"+ Zhuanye +"', '"+ classname +"')", conn); Insertcmd.                         ExecuteNonQuery (); //Break ;                    }                }                Else{Errorcount++; }} Response.Write ("<script language= ' javascript ' >alert ('"+ Insertcount +"Data import successfully! "+ Updatecount +"Data updated successfully! "+ Errorcount +"The data section information is empty and not imported! ');</script>"); }        //catch (Exception exp)//{        //Response.Write ("<script language= ' JavaScript ' >alert (' Import failed!        ');</script> "); //}        finally{Conn.        Close (); }        }//determine the upload file and save the file    PrivateString TypeName (FileUpload fileloads) {stringFullfilename =fileloads.        Postedfile.filename; stringfilename = fullfilename. Substring (Fullfilename. LastIndexOf ("\\\\") +1); stringType = Fullfilename. Substring (Fullfilename. LastIndexOf (".") +1); stringMurl =""; if(Type = ="xls") {fileloads. Postedfile.saveas (Server.MapPath ("Excel") +"\\\\"+filename); Murl= (Server.MapPath ("Excel") +"\\\\"+filename).        ToString (); }        Else{Response.Write ("<script language= ' javascript ' >alert (' import file format is incorrect! '); </script>"); }        returnMurl; } //Database import datasets DataSet     PrivateDataSet Xsldata (stringfilepath) {        stringStrcon ="Provider=Microsoft.Jet.OLEDB.4.0;Data source="+ filepath +"; Extended properties= ' Excel 8.0;imex=1 '"; System.Data.OleDb.OleDbConnection Conn=NewSystem.Data.OleDb.OleDbConnection (Strcon); stringstrcom ="SELECT * FROM [sheet1$]";        Conn.Open (); System.Data.OleDb.OleDbDataAdapter mycommand=NewSystem.Data.OleDb.OleDbDataAdapter (strcom, Conn); DataSet DS=NewDataSet (); Mycommand.fill (DS,"[sheet1$]");        Conn.close (); returnds; }}

Excel import Database in ASP.

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.