将上传的access数据库或xml文件存入数据库中(仅是个人资料备份)

private void btnUpload_Click(object sender, System.EventArgs e)
        {
            if(this.File1.PostedFile.FileName==null)
            {
                 Response.Write("<script>alert('请选择要上传的文件!');</script>");
            }
            else
            {
                string fileName    = this.File1.PostedFile.FileName;
                string fileType    = this.File1.PostedFile.ContentType;
               
                //判断上传的文件的类型,只能是access文件与xml文件
                if(fileType=="application/msaccess" || fileType=="application/x-xml")
                {
                    string firstName   = fileName.Substring(fileName.LastIndexOf("\\")+1).Substring(0,fileName.Substring(fileName.LastIndexOf("\\")+1).Length-4);
                    string lastName    = fileName.Substring(fileName.LastIndexOf(".")+1);
                    string fileNewName = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + lastName;
                    string filePath    = Server.MapPath("uploadfile/") + fileNewName;

                    if(fileType=="application/msaccess")
                    {
                        //对access文件进行操作
                        this.File1.PostedFile.SaveAs(filePath);
                       
                        //判断上传的文件是否存在
                        if(File.Exists(Server.MapPath(@"uploadfile/" + fileNewName))==true)
                        {
                            //判断主数据库中是否已经存在此次上传的单位基本信息
                            string strAce1 = "select count(*) from Ent_Baseinfo where Zzjgdm=(select top 1 Zzjgdm from openrowset('Microsoft.Jet.OLEDB.4.0','"+filePath+"';'admin';'',Ent_Baseinfo))";
                            if(cdb.isInfo(strAce1)>0)
                            {
                                File.Delete(Server.MapPath("uploadfile/" + fileNewName));
                                Response.Write("<script>alert('单位基本信息已经存入主数据库,请确认!');</script>");
                            }
                            else
                            {
                                string strInAce1 = "insert into Ent_Baseinfo (Zzjgdm,Qymc,Qylx,Zzzsbh,Zzdj,Zcd,Lxdh,Yyzzh,Qyxz,UserId,Zcxzqh,Qyfr)select Zzjgdm,Qymc,Qylx,Zzzsbh,Zzdj,Zcd,Lxdh,Yyzzh,Qyxz,UserId,Zcxzqh,Qyfr from openrowset('Microsoft.Jet.OLEDB.4.0','"+filePath+"';'admin';'',Ent_Baseinfo)";
                                try
                                {
                                    cdb.myExecute(strInAce1);
                                    Response.Write("<script>alert('单位基本信息已经成功导入!');</script>");
                                }
                                catch(Exception ex)
                                {
                                    File.Delete(Server.MapPath("uploadfile/" + fileNewName));
                                    string errMassage = "错误:"+ ex.Message.Substring( 0 , ex.Message.IndexOf( "。" )).Replace( "'" , " " ).Replace( '"' , ' ' );
                                    Response.Write("<script>alert('"+errMassage+"')</script>");
                                }
                               
                            }
                            //判断主数据库中是否已经存在此次上传的单位不良信息
                            string strAce2 = "select count(*) from Ent_BadCredit where Zzjgdm=(select top 1 Zzjgdm from openrowset('Microsoft.Jet.OLEDB.4.0','"+filePath+"';'admin';'',Ent_BadCredit))";
                            if(cdb.isInfo(strAce2)>0)
                            {
                                File.Delete(Server.MapPath("uploadfile/" + fileNewName));
                                Response.Write("<script>alert('企业不良信息已经存入主数据库,请确认!');</script>");
                            }
                            else
                            {
                                string strInAce2 = "insert into Ent_BadCredit (Zzjgdm,Qylx,Gcmc,Gcdz,Gcjsdw,Xwdm,Xwms,Fsrq,Cfjd,Cfjg,Cfrq,Gcxzqh,UserId,WebSite,Qymc)select Zzjgdm,Qylx,Gcmc,Gcdz,Gcjsdw,Xwdm,convert(varchar(500),Xwms),Fsrq,convert(varchar(500),Cfjd),Cfjg,Cfrq,Gcxzqh,UserId,convert(varchar(500),Xzwjwz),Qymc from openrowset('Microsoft.Jet.OLEDB.4.0','"+filePath+"';'admin';'',Ent_BadCredit)";
                                try
                                {
                                    cdb.myExecute(strInAce2);
                                    Response.Write("<script>alert('企业不良信息已经成功导入!');</script>");
                                }
                                catch(Exception ex)
                                {
                                    File.Delete(Server.MapPath("uploadfile/" + fileNewName));
                                    string errMassage = "错误:"+ ex.Message.Substring( 0 , ex.Message.IndexOf( "。" )).Replace( "'" , " " ).Replace( '"' , ' ' );
                                    Response.Write("<script>alert('"+errMassage+"')</script>");
                                }
                            }
                        }
                        else
                        {
                             Response.Write("<script>alert('对不起,文件上传失败,请确认!');</script>");
                        }
                    }
                    else
                    {
                        //对上传的xml文件进行存入主数据库

                        this.File1.PostedFile.SaveAs(filePath);
                       
                        //判断上传的文件是否存在
                        if(File.Exists(Server.MapPath(@"uploadfile/" + fileNewName))==true)
                        {
                            DataSet ds = new DataSet();
                            ds.ReadXml(Server.MapPath("uploadfile/" + fileNewName));
                            DataTable dt = ds.Tables[0];

                            //根据DataTable中的字段来名来判断是 企业信息表 还是 企业不良信息表
                            if(dt.Columns[11].ColumnName.Equals("Qyfr"))
                            {

                                //判断上传的企业信息表中的内容是否已经存在,如果存在则返回,不存在写入数据库
                                string strXml1 = "select count(*) from Ent_Baseinfo where Zzjgdm='"+dt.Rows[0]["Zzjgdm"].ToString()+"'";
                                if(cdb.isInfo(strXml1)>0)
                                {
                                    File.Delete(Server.MapPath("uploadfile/" + fileNewName));
                                    Response.Write("<script>alert('此企业基本情况表已经上传过了!');</script>");
                                }
                                else
                                {
                                    for(int i=0;i<dt.Rows.Count;i++)
                                    {
                                        string strInXml1 = "insert into Ent_Baseinfo(Zzjgdm,Qymc,Qylx,Zzzsbh,Zzdj,Zcd,Lxdh,Yyzzh,Qyxz,UserId,Zcxzqh,Qyfr)values('"+dt.Rows[i]["Zzjgdm"].ToString()+"','"+dt.Rows[i]["Qymc"].ToString()+"',"+Convert.ToInt32(dt.Rows[i]["Qylx"].ToString())+",'"+dt.Rows[i]["Zzzsbh"].ToString()+"','"+dt.Rows[i]["Zzdj"].ToString()+"','"+dt.Rows[i]["Zcd"].ToString()+"','"+dt.Rows[i]["Lxdh"].ToString()+"','"+dt.Rows[i]["Yyzzh"].ToString()+"',"+Convert.ToInt32(dt.Rows[i]["Qyxz"].ToString())+","+Convert.ToInt32(dt.Rows[i]["UserId"].ToString())+",'"+dt.Rows[i]["Zcxzqh"].ToString()+"','"+dt.Rows[i]["Qyfr"].ToString()+"')";
                                        try
                                        {
                                            cdb.myExecute(strInXml1);
                                            Response.Write("<script>alert('此企业基本情况表上传成功!');</script>");
                                        }
                                        catch(Exception ex)
                                        {
                                            File.Delete(Server.MapPath("uploadfile/" + fileNewName));
                                            string errMassage = "错误:"+ ex.Message.Substring( 0 , ex.Message.IndexOf( "。" )).Replace( "'" , " " ).Replace( '"' , ' ' );
                                            Response.Write("<script>alert('"+errMassage+"')</script>");
                                        }
                                    }
                                }
                            }
                            else if(dt.Columns[9].ColumnName.Equals("Cfjg"))
                            {

                                //判断上传的企业不良信息表中的内容是否已经存在,如果存在则返回,不存在写入数据库
                                string strXml2 = "select count(*) from Ent_BadCredit where Zzjgdm='"+dt.Rows[0]["Zzjgdm"].ToString()+"'";
                                if(cdb.isInfo(strXml2)>0)
                                {
                                    File.Delete(Server.MapPath("uploadfile/" + fileNewName));
                                    Response.Write("<script>alert('此企业不良信息表已经上传过了!');</script>");
                                }
                                else
                                {
                                    for(int i=0;i<dt.Rows.Count;i++)
                                    {
                                       
                                        try
                                        {
                                            string strInXml2 = "insert into Ent_BadCredit(Zzjgdm,Qylx,Gcmc,Gcdz,Gcjsdw,Xwdm,Xwms,Fsrq,Cfjd,Cfjg,Cfrq,Gcxzqh,UserId,WebSite,Qymc)values('"+dt.Rows[i]["Zzjgdm"].ToString()+"',"+Convert.ToInt32(dt.Rows[i]["Qylx"].ToString())+",'"+dt.Rows[i]["Gcmc"].ToString()+"','"+dt.Rows[i]["Gcdz"].ToString()+"','"+dt.Rows[i]["Gcjsdw"].ToString()+"','"+dt.Rows[i]["Xwdm"].ToString()+"','"+dt.Rows[i]["Xwms"].ToString()+"','"+dt.Rows[i]["Fsrq"].ToString()+"','"+dt.Rows[i]["Cfjd"].ToString()+"','"+dt.Rows[i]["Cfjg"].ToString()+"','"+dt.Rows[i]["Cfrq"].ToString()+"','"+dt.Rows[i]["Gcxzqh"].ToString()+"',"+Convert.ToInt32(dt.Rows[i]["UserId"].ToString())+",'"+dt.Rows[i]["Xzwjwz"].ToString()+"','"+dt.Rows[i]["Qymc"].ToString()+"')";
                                            cdb.myExecute(strInXml2);
                                            Response.Write("<script>alert('此企业不良信息表上传成功!');</script>");
                                        }
                                        catch(Exception ex)
                                        {
                                            File.Delete(Server.MapPath("uploadfile/" + fileNewName));
                                            string errMassage = "错误:"+ ex.Message.Substring( 0 , ex.Message.IndexOf( "。" )).Replace( "'" , " " ).Replace( '"' , ' ' );
                                            Response.Write("<script>alert('"+errMassage+"')</script>");
                                        }
                                    }
                                }
                            }
                            else
                            {
                                Response.Write("<script>alert('对不起,Xml文件上传格式不对,请确认!');</script>");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('对不起,文件上传失败,请确认!');</script>");
                        }
                       
                    }
                }
                else
                {
                   Response.Write("<script>alert('你选择的文件类型不对,请确认!!');</script>");
                }
            }
        }

转载于:https://www.cnblogs.com/chen79/archive/2007/12/17/1003085.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值