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.