コード例 #1
0
    public void SaveDataToDB(string FullPath, string FileName, string excelFileExtension)
    {
        SqlTransaction transaction = null;

        try {
            DataTable dt = null;
            dt = this.GetDataSet(FullPath, excelFileExtension).Tables[0];
            if (dt.Rows.Count <= 1)
            {
                PageUtility.ShowModelDlg(this.Page, "文件中没有任何记录,请重新选择");
                return;
            }
            CustomerTableAdapter        TACustomer        = new CustomerTableAdapter();
            ImportLogTableAdapter       TAImportLog       = new ImportLogTableAdapter();
            ImportLogDetailTableAdapter TAImportLogDetail = new ImportLogDetailTableAdapter();
            MasterDataBLL mdBLL = new MasterDataBLL();

            transaction = TableAdapterHelper.BeginTransaction(TACustomer);
            TableAdapterHelper.SetTransaction(TAImportLog, transaction);
            TableAdapterHelper.SetTransaction(TAImportLogDetail, transaction);
            //存储log信息
            ImportDS.ImportLogDataTable logTable = new ImportDS.ImportLogDataTable();
            ImportDS.ImportLogRow       logRow   = logTable.NewImportLogRow();

            int    stuffUserID = ((AuthorizationDS.StuffUserRow)Session["StuffUser"]).StuffUserId;
            string fullname    = this.fileUpLoad.PostedFile.FileName.ToString();
            string tmpFile     = fullname.Remove(0, fullname.LastIndexOf("\\") + 1);
            logRow.FileName     = tmpFile;
            logRow.ImportDate   = DateTime.Now;
            logRow.ImportUserID = stuffUserID;
            logRow.ImportType   = 3;
            logRow.TotalCount   = dt.Rows.Count - 1;
            logRow.SuccessCount = dt.Rows.Count - 1;
            logRow.FailCount    = 0;
            logTable.AddImportLogRow(logRow);
            TAImportLog.Update(logTable);

            //处理每条明细
            ERS.CustomerDataTable             tbCustomer           = new ERS.CustomerDataTable();
            ImportDS.ImportLogDetailDataTable ImportLogDetailTable = new ImportDS.ImportLogDetailDataTable();
            int    row_count  = dt.Rows.Count;
            string errorInfor = string.Empty;
            //  int expenseTypeID = int.Parse(ExpenseTypeDDL.SelectedValue);
            //开始处理每条明细
            for (int i = 1; i <= row_count - 1; i++)
            {
                if (CheckData(dt.Rows[i]) != null)
                {
                    errorInfor = "第" + (i + 1) + "行有错:" + CheckData(dt.Rows[i]);
                    ImportDS.ImportLogDetailRow ImportDetailRow = ImportLogDetailTable.NewImportLogDetailRow();
                    ImportDetailRow.LogID = logRow.LogID;
                    ImportDetailRow.Line  = i + 1;
                    ImportDetailRow.Error = errorInfor;
                    ImportLogDetailTable.AddImportLogDetailRow(ImportDetailRow);
                    logRow.FailCount    = logRow.FailCount + 1;
                    logRow.SuccessCount = logRow.SuccessCount - 1;
                    TAImportLog.Update(logRow);
                    continue;
                }
                else
                {
                    DataRow row              = dt.Rows[i];
                    string  CustomerNo       = row[0].ToString().Trim();
                    string  CustomerName     = row[1].ToString().Trim();
                    string  ProvinceName     = row[2].ToString().Trim();
                    string  CityName         = row[3].ToString().Trim();
                    string  CustomerTypeName = row[4].ToString().Trim();
                    string  ChannelTypeName  = row[5].ToString().Trim();
                    string  OUName           = row[6].ToString().Trim();
                    string  BudgetOUName     = row[7].ToString().Trim();

                    ERS.CustomerDataTable tbCustomerTemp = mdBLL.GetCustomerByCustomerName(CustomerName);

                    if (tbCustomerTemp != null && tbCustomerTemp.Count > 0)
                    {
                        errorInfor = "第" + (i + 1) + "行有错:已存在客户《" + CustomerName + "》";
                        ImportDS.ImportLogDetailRow ImportDetailRow = ImportLogDetailTable.NewImportLogDetailRow();
                        ImportDetailRow.LogID = logRow.LogID;
                        ImportDetailRow.Line  = i + 1;
                        ImportDetailRow.Error = errorInfor;
                        ImportLogDetailTable.AddImportLogDetailRow(ImportDetailRow);
                        logRow.FailCount    = logRow.FailCount + 1;
                        logRow.SuccessCount = logRow.SuccessCount - 1;
                        TAImportLog.Update(logRow);
                        continue;
                    }

                    //ERS.ProvinceDataTable tbProvince = mdBLL.GetProvinceByProvinceName(ProvinceName);

                    //if (tbProvince == null || tbProvince.Count == 0) {
                    //    errorInfor = "第" + (i + 1) + "行有错:找不到省份《" + ProvinceName + "》";
                    //    ImportDS.ImportLogDetailRow ImportDetailRow = ImportLogDetailTable.NewImportLogDetailRow();
                    //    ImportDetailRow.LogID = logRow.LogID;
                    //    ImportDetailRow.Line = i + 1;
                    //    ImportDetailRow.Error = errorInfor;
                    //    ImportLogDetailTable.AddImportLogDetailRow(ImportDetailRow);
                    //    logRow.FailCount = logRow.FailCount + 1;
                    //    logRow.SuccessCount = logRow.SuccessCount - 1;
                    //    TAImportLog.Update(logRow);
                    //    continue;
                    //}

                    ERS.CityDataTable tbCity = mdBLL.GetCityByCityName(CityName);

                    if (tbCity == null || tbCity.Count == 0)
                    {
                        errorInfor = "第" + (i + 1) + "行有错:找不到城市《" + CityName + "》";
                        ImportDS.ImportLogDetailRow ImportDetailRow = ImportLogDetailTable.NewImportLogDetailRow();
                        ImportDetailRow.LogID = logRow.LogID;
                        ImportDetailRow.Line  = i + 1;
                        ImportDetailRow.Error = errorInfor;
                        ImportLogDetailTable.AddImportLogDetailRow(ImportDetailRow);
                        logRow.FailCount    = logRow.FailCount + 1;
                        logRow.SuccessCount = logRow.SuccessCount - 1;
                        TAImportLog.Update(logRow);
                        continue;
                    }

                    ERS.CustomerTypeDataTable tbCustomerType = mdBLL.GetCustomerTypeByCustomerTypeName(CustomerTypeName);

                    if (tbCustomerType == null || tbCustomerType.Count == 0)
                    {
                        errorInfor = "第" + (i + 1) + "行有错:找不到客户类型《" + CustomerTypeName + "》";
                        ImportDS.ImportLogDetailRow ImportDetailRow = ImportLogDetailTable.NewImportLogDetailRow();
                        ImportDetailRow.LogID = logRow.LogID;
                        ImportDetailRow.Line  = i + 1;
                        ImportDetailRow.Error = errorInfor;
                        ImportLogDetailTable.AddImportLogDetailRow(ImportDetailRow);
                        logRow.FailCount    = logRow.FailCount + 1;
                        logRow.SuccessCount = logRow.SuccessCount - 1;
                        TAImportLog.Update(logRow);
                        continue;
                    }

                    ERS.ChannelTypeDataTable tbChannelType = mdBLL.GetChannelTypeByChannelTypeName(ChannelTypeName);

                    if (tbChannelType == null || tbChannelType.Count == 0)
                    {
                        errorInfor = "第" + (i + 1) + "行有错:找不到客户渠道《" + ChannelTypeName + "》";
                        ImportDS.ImportLogDetailRow ImportDetailRow = ImportLogDetailTable.NewImportLogDetailRow();
                        ImportDetailRow.LogID = logRow.LogID;
                        ImportDetailRow.Line  = i + 1;
                        ImportDetailRow.Error = errorInfor;
                        ImportLogDetailTable.AddImportLogDetailRow(ImportDetailRow);
                        logRow.FailCount    = logRow.FailCount + 1;
                        logRow.SuccessCount = logRow.SuccessCount - 1;
                        TAImportLog.Update(logRow);
                        continue;
                    }

                    AuthorizationDS.OrganizationUnitDataTable ouTable = new OUTreeBLL().GetDataByOrganizationUnitName(OUName);

                    if (ouTable == null || ouTable.Count == 0)
                    {
                        errorInfor = "第" + (i + 1) + "行有错:系统中找不到此部门《" + OUName + "》";
                        ImportDS.ImportLogDetailRow ImportDetailRow = ImportLogDetailTable.NewImportLogDetailRow();
                        ImportDetailRow.LogID = logRow.LogID;
                        ImportDetailRow.Line  = i + 1;
                        ImportDetailRow.Error = errorInfor;
                        ImportLogDetailTable.AddImportLogDetailRow(ImportDetailRow);
                        logRow.FailCount    = logRow.FailCount + 1;
                        logRow.SuccessCount = logRow.SuccessCount - 1;
                        TAImportLog.Update(logRow);
                        continue;
                    }

                    if (ouTable.Count > 1)
                    {
                        errorInfor = "第" + (i + 1) + "行有错:系统中找到多个此名称的部门《" + OUName + "》";
                        ImportDS.ImportLogDetailRow ImportDetailRow = ImportLogDetailTable.NewImportLogDetailRow();
                        ImportDetailRow.LogID = logRow.LogID;
                        ImportDetailRow.Line  = i + 1;
                        ImportDetailRow.Error = errorInfor;
                        ImportLogDetailTable.AddImportLogDetailRow(ImportDetailRow);
                        logRow.FailCount    = logRow.FailCount + 1;
                        logRow.SuccessCount = logRow.SuccessCount - 1;
                        TAImportLog.Update(logRow);
                        continue;
                    }

                    AuthorizationDS.OrganizationUnitDataTable BudgetOUTable = new OUTreeBLL().GetDataByOrganizationUnitName(BudgetOUName);

                    if (BudgetOUTable == null || BudgetOUTable.Count == 0)
                    {
                        errorInfor = "第" + (i + 1) + "行有错:系统中找不到此部门《" + BudgetOUName + "》";
                        ImportDS.ImportLogDetailRow ImportDetailRow = ImportLogDetailTable.NewImportLogDetailRow();
                        ImportDetailRow.LogID = logRow.LogID;
                        ImportDetailRow.Line  = i + 1;
                        ImportDetailRow.Error = errorInfor;
                        ImportLogDetailTable.AddImportLogDetailRow(ImportDetailRow);
                        logRow.FailCount    = logRow.FailCount + 1;
                        logRow.SuccessCount = logRow.SuccessCount - 1;
                        TAImportLog.Update(logRow);
                        continue;
                    }

                    if (BudgetOUTable.Count > 1)
                    {
                        errorInfor = "第" + (i + 1) + "行有错:系统中找到多个此名称的部门《" + BudgetOUName + "》";
                        ImportDS.ImportLogDetailRow ImportDetailRow = ImportLogDetailTable.NewImportLogDetailRow();
                        ImportDetailRow.LogID = logRow.LogID;
                        ImportDetailRow.Line  = i + 1;
                        ImportDetailRow.Error = errorInfor;
                        ImportLogDetailTable.AddImportLogDetailRow(ImportDetailRow);
                        logRow.FailCount    = logRow.FailCount + 1;
                        logRow.SuccessCount = logRow.SuccessCount - 1;
                        TAImportLog.Update(logRow);
                        continue;
                    }

                    ERS.CustomerRow rowCustomer = tbCustomer.NewCustomerRow();

                    rowCustomer.CustomerNo              = CustomerNo;
                    rowCustomer.CustomerName            = CustomerName;
                    rowCustomer.CityID                  = tbCity[0].CityID;
                    rowCustomer.CustomerTypeID          = tbCustomerType[0].CustomerTypeID;
                    rowCustomer.ChannelTypeID           = tbChannelType[0].ChannelTypeID;
                    rowCustomer.OrganizationUnitID      = ouTable[0].OrganizationUnitId;
                    rowCustomer.ApplyOrganizationUnitID = BudgetOUTable[0].OrganizationUnitId;
                    rowCustomer.IsActive                = true;

                    tbCustomer.AddCustomerRow(rowCustomer);
                    TACustomer.Update(rowCustomer);
                }
            }
            TAImportLog.Update(logRow);
            TAImportLogDetail.Update(ImportLogDetailTable);
            transaction.Commit();
            string returnString = "成功导入" + logRow.SuccessCount.ToString() + "条信息";
            PageUtility.ShowModelDlg(this.Page, returnString);
        } catch (Exception ex) {
            if (transaction != null)
            {
                transaction.Rollback();
            }
            PageUtility.ShowModelDlg(this.Page, "Save Fail!" + ex.ToString());
        } finally {
            if (transaction != null)
            {
                transaction.Dispose();
            }
        }
    }