You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
5.3 KiB

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
{
/// <summary>
/// 如果数据库、数据表不存在,则创建。失败则返回失败信息。
/// </summary>
/// <returns>失败则返回失败信息。</returns>
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;
}
/// <summary>
/// 如果数据库不存在,则创建数据库。如果存在,则直接返回true
/// <para>执行成功,返回true;否则返回false</para>
/// </summary>
/// <returns>执行成功,返回true;否则返回false</returns>
public static bool CreateDataBaseIfNotExist()//数据库不存在时,创建数据库
{
string errorMessage;
return CreateDataBaseIfNotExist(out errorMessage);
}
/// <summary>
/// 如果数据库不存在,则创建数据库。如果存在,则直接返回true
/// <para>执行成功,返回true;否则返回false</para>
/// </summary>
/// <returns>执行成功,返回true;否则返回false</returns>
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;
}
}
/// <summary>
/// 如果数据表不存在,则创建。失败则返回失败信息。
/// </summary>
/// <returns>失败则返回失败信息。</returns>
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;
}
/// <summary>
/// 如果数据表不存在,则创建数据表。如果存在,则直接返回true
/// <para>执行成功,返回true;否则返回false</para>
/// </summary>
/// <returns>执行成功,返回true;否则返回false</returns>
public static bool CreateTableIfNotExist()//表不存在时,创建表
{
string errorMessage;
return CreateTableIfNotExist(out errorMessage);
}
/// <summary>
/// 如果数据表不存在,则创建数据表。如果存在,则直接返回true
/// <para>执行成功,返回true;否则返回false</para>
/// </summary>
/// <returns>执行成功,返回true;否则返回false</returns>
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;
}
///// <summary>
///// 获取连接本地数据的字符串
///// </summary>
public static string ConnectionStr { get { return SQLExecuteUtil.GetConnectionString(TableNames.); } }
}
}