using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using FireBirdUtil.SqlUtils; using WeighBusiness.BO.CreateTables; namespace WeighBusiness.Utils { public class CreateUtil { /// /// 如果数据库、数据表不存在,则创建。失败则返回失败信息。 /// /// 失败则返回失败信息。 public static string CreateDataBaseAndTable() { try { string errorMessage; var success = CreateUtil.CreateDataBaseIfNotExist(out errorMessage); if (!success) { return "数据库创建失败" + Environment.NewLine + errorMessage; } success = CreateUtil.CreateTableIfNotExist(out errorMessage); if (!success) return "数据表创建失败" + Environment.NewLine + errorMessage; } catch (Exception ex) { return "在创建数据库时碰到错误:" + ex.Message; } return string.Empty; } /// /// 如果数据库不存在,则创建数据库。如果存在,则直接返回true /// 执行成功,返回true;否则返回false /// /// 执行成功,返回true;否则返回false public static bool CreateDataBaseIfNotExist()//数据库不存在时,创建数据库 { string errorMessage; return CreateDataBaseIfNotExist(out errorMessage); } /// /// 如果数据库不存在,则创建数据库。如果存在,则直接返回true /// 执行成功,返回true;否则返回false /// /// 执行成功,返回true;否则返回false public static bool CreateDataBaseIfNotExist(out string errorMessage)//数据库不存在时,创建数据库 { errorMessage = string.Empty; try { var folder = new FileInfo(TableNames.数据库).Directory.FullName; if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } SQLExecuteUtil.CreateDataBaseIfNotExist(TableNames.数据库); return true; } catch (Exception ex) { errorMessage = ex.ToString(); return false; } } /// /// 如果数据表不存在,则创建。失败则返回失败信息。 /// /// 失败则返回失败信息。 public static string CreateTableIfNeed() { try { string errorMessage; var success = CreateUtil.CreateTableIfNotExist(out errorMessage); if (!success) return "数据表创建失败" + Environment.NewLine + errorMessage; } catch (Exception ex) { return "在创建数据表时碰到错误:" + ex.Message; } return string.Empty; } /// /// 如果数据表不存在,则创建数据表。如果存在,则直接返回true /// 执行成功,返回true;否则返回false /// /// 执行成功,返回true;否则返回false public static bool CreateTableIfNotExist()//表不存在时,创建表 { string errorMessage; return CreateTableIfNotExist(out errorMessage); } /// /// 如果数据表不存在,则创建数据表。如果存在,则直接返回true /// 执行成功,返回true;否则返回false /// /// 执行成功,返回true;否则返回false public static bool CreateTableIfNotExist(out string errorMessage)//表不存在时,创建表 { errorMessage = string.Empty; if (!new CreateWeightInfo(TableNames.称重记录).Create(out errorMessage)) return false; if (!new CreateUserTable(TableNames.用户表).Create(out errorMessage)) return false; if (!new CreateEmployeeTable(TableNames.ERP员工).Create(out errorMessage)) return false; if (!new CreateProductPlanTable(TableNames.生产计划).Create(out errorMessage)) return false; if (!new CreateButcherDetail(TableNames.生产计划分割明细).Create(out errorMessage)) return false; if (!new CreateProductBatchTable(TableNames.生产计划生产批次).Create(out errorMessage)) return false; if (!new CreateProductCatalog(TableNames.客户端产品分类).Create(out errorMessage)) return false; if (!new CreateProductCatalog_Detail(TableNames.客户端产品分类明细).Create(out errorMessage)) return false; if (!new CreateProductTeam(TableNames.生产班组).Create(out errorMessage)) return false; if (!new CreateProductTeamOutDetail(TableNames.生产班组产出明细).Create(out errorMessage)) return false; if (!new CreateSelectedProductCatalogGoods(TableNames.客户端产品分类配制存货).Create(out errorMessage)) return false; if (!new CreateButcherDetailFinishNum(TableNames.生产计划分割明细完工数量).Create(out errorMessage)) return false; if (!new CreateGoodsWeightFloat_Detail(TableNames.存货称重浮动配置明细).Create(out errorMessage)) return false; if (!new CreateGoodsWeightFloat(TableNames.存货称重浮动配置).Create(out errorMessage)) return false; return true; } ///// ///// 获取连接本地数据的字符串 ///// public static string ConnectionStr { get { return SQLExecuteUtil.GetConnectionString(TableNames.数据库); } } } }