EXCEL导入数据库

  public static void ExcelToDataBase(IDbSession cn, DataSet dataSet, long ManagerId)
        {
            if (cn.Transaction == null)
            {
                throw new BusinessException("调用此方法需要先开启事务");
            }
            if (dataSet != null)
            {
                //表
                for (int i = 0; i < dataSet.Tables.Count; i++)
                {


                    //1.找分类
                    DataTable table = dataSet.Tables[i];
                    QueryExpression q1 = new QueryExpression();
                    q1.Selects.Add(Category_.CategoryId);
                    q1.Selects.Add(Category_.CategoryName);
                    q1.Wheres.Add(Category_.CategoryName.TEqual(table.TableName));
                    q1.Wheres.Add(Category_.IsActive.TEqual(1));
                    Categorys categorys = CategoryService.GetCategorys(cn, q1);
                    if (categorys != null && categorys.Items.Count > 0)
                    {
                        Category category = categorys.Items[0];
                        ProductGlobal productGlobal = new ProductGlobal();
                        //行
                        for (int j = 1; j < table.Rows.Count; j++)
                        {
                            DataRow row = table.Rows[j];
                            string ProductName = row[0].ToString().Trim();//产品名称
                            string Spec = row[1].ToString().Trim();//规格
                            string Unit = row[2].ToString().Trim();//计价单位
                            string DisplayUnitTypeId = row[3].ToString().Trim();//单位是否一致 
                            string DisplayUnit = row[4].ToString().Trim();//购买单位
                            string UnitAmount = row[5].ToString().Trim();//计量数
                            if (string.IsNullOrEmpty(ProductName))
                            {
                                throw new BusinessException("产品名称不能为空");
                            }
                            if (string.IsNullOrEmpty(DisplayUnitTypeId))
                            {
                                throw new BusinessException("单位是否一致不能为空");
                            }
                            if ("否".Equals(DisplayUnitTypeId))
                            {
                                productGlobal.DisplayUnitTypeId = DisplayUnitType.None;
                            }
                            else if ("是".Equals(DisplayUnitTypeId))
                            {
                                productGlobal.DisplayUnitTypeId = DisplayUnitType.Replace;
                                if (string.IsNullOrEmpty(DisplayUnit))
                                {
                                    throw new BusinessException("单位不一致时,购买单位不能为空");
                                }
                                if (string.IsNullOrEmpty(UnitAmount))
                                {
                                    throw new BusinessException("单位不一致时,计量数不能为空");
                                }
                                try
                                {
                                    productGlobal.UnitAmount = decimal.Parse(UnitAmount);
                                }
                                catch (Exception e)
                                {
                                    throw new BusinessException("计量数必须为数字");
                                }
                            }
                            else
                            {
                                throw new BusinessException("单位是否不一致请填写是或否");
                            }
                            productGlobal.ProductName = ProductName;
                            productGlobal.Spec = Spec;
                            productGlobal.Unit = ProductName;
                            productGlobal.DisplayUnit = DisplayUnit;


                            //2.去重、添加商品分类
                            ConditionExpressionCollection cs = new ConditionExpressionCollection();
                            cs.Add(ProductGlobal_.ProductName.TEqual(ProductName));
                            cs.Add(ProductGlobal_.Spec.TEqual(Spec));
                            cs.Add(ProductGlobal_.IsActive.TEqual(true));
                            if (cn.Exists<ProductGlobal>(cs))
                            {
                                throw new BusinessException(ProductName + "-" + Spec + ":已经存在,不能重复添加");
                            }
                            productGlobal.CategoryId = category.CategoryId;
                            productGlobal.ManagerId = ManagerId;
                            ProductService.InsertProductGlobal(cn, productGlobal);
                        }
                    }
                    else
                    {
                        throw new BusinessException(table.TableName + "-分类不存在");
                    }


                }


            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值