关闭

简单的EXCL到入到数据库代码

250人阅读 评论(0) 收藏 举报

string source=this.FileUpload1.PostedFile.FileName;
         string getextend = System.IO.Path.GetExtension(source);//得到

         if (source == String.Empty || getextend != ".xls")
         {
             Response.Write("<script>alert('')</script>");

         }
         else
         {
             string strconn = "provider=microsoft.jet.oledb.4.0;data source=" + source + ";Extended Properties=EXCEL 8.0";
             string str_select = "select * from [Sheet1$]";
             OleDbConnection con = new OleDbConnection(strconn);
             OleDbDataAdapter sda = new OleDbDataAdapter(str_select, con);
             DataSet ds = new DataSet();
             sda.Fill(ds, "view");
             this.gvshow.DataSource = ds.Tables[0].DefaultView;
             this.gvshow.DataBind();
             int count=ds.Tables[0].Rows.Count;
             if (count > 0)
             {
                 DataTable dt = new DataTable();
                 dt.Columns.Add("卡号");
                 dt.Columns.Add("密码");
                 dt = ds.Tables[0];
                 DataRow[] rows = dt.Select();
                 string sql="";
                 foreach(DataRow dr in rows)
                 {
                     string pid = dr[0].ToString();
                     string pwd=dr[1].ToString();

                    
                      sql+="insert into card values('"+pid+"','"+pwd+"');";
               
                
                 }
             SqlConnection con2 = new SqlConnection("server=tamonga;database=test;uid=sa;pwd=");
                      con2.Open();
                      SqlCommand cmd = new SqlCommand(sql, con2);
                      int counts = cmd.ExecuteNonQuery();
                      Response.Write("<scritp>alert('成功插入"+counts.ToString()+"')</script>");
                
                 }
               

            
             }

 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8927次
    • 积分:232
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档