Explorar el Código

程序启动时检查数据库环境

zoulei hace 11 meses
padre
commit
59588b02db
Se han modificado 3 ficheros con 58 adiciones y 20 borrados
  1. 2 2
      XdCxRhDW.App/App.config
  2. 46 15
      XdCxRhDW.App/MainForm.cs
  3. 10 3
      XdCxRhDW.Repostory/EFContext/RHDWContext.cs

+ 2 - 2
XdCxRhDW.App/App.config

@@ -1,8 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
 <configuration>
 	<connectionStrings>
-		<add name="DbCon" connectionString="Data Source=|DataDirectory|\Database.db" providerName="System.Data.SQLite.EF6" />
-		<add name="LogDbCon" connectionString="Data Source=|DataDirectory|\Log.db" providerName="System.Data.SQLite.EF6" />
+		<add name="DbCon" connectionString="Data Source=Database.db" providerName="System.Data.SQLite.EF6" />
+		<add name="LogDbCon" connectionString="Data Source=Log.db" providerName="System.Data.SQLite.EF6" />
 	</connectionStrings>
 	<appSettings>
 		<!--程序标题-->

+ 46 - 15
XdCxRhDW.App/MainForm.cs

@@ -40,6 +40,7 @@ namespace XdCxRhDW
     public partial class MainForm : DevExpress.XtraBars.Ribbon.RibbonForm
     {
         Dictionary<string, Type> ctrlTypes = new Dictionary<string, Type>();
+        string errmsg;
         public MainForm()
         {
             InitializeComponent();
@@ -61,6 +62,8 @@ namespace XdCxRhDW
             ctrlTypes.Add("信号仿真", typeof(SignalEmulation));
             ctrlTypes.Add("服务状态", typeof(CtrlSvrs));
             ctrlTypes.Add("服务日志", typeof(CtrlSvrLog));
+            DxHelper.WaitHelper.UpdateSplashMessage("正在检查数据库环境...");
+            errmsg = CheckDb();
 
         }
         private string text;
@@ -68,8 +71,9 @@ namespace XdCxRhDW
         {
             this.text = this.Text;
             this.HtmlText = $"<size=12>{this.text}";
-            if (!CheckDb())
+            if (!string.IsNullOrWhiteSpace(errmsg))
             {
+                MsgBoxHelper.ShowError(errmsg);
                 Application.Exit();
                 return;
             }
@@ -116,7 +120,7 @@ namespace XdCxRhDW
             {
                 try
                 {
-                    int interval = 10000;
+                    int interval = 30000;
                     CpuMonitor sys = new CpuMonitor();
                     const int GB_DIV = 1024 * 1024 * 1024;
                     while (true)
@@ -131,24 +135,51 @@ namespace XdCxRhDW
             });
 
         }
-        private bool CheckDb()
+        private string CheckDb()
         {
-            RHDWContext db = new RHDWContext();
-            string notExistTableName = db.CheckTableExist();
-            if (!string.IsNullOrWhiteSpace(notExistTableName))
+            using (RHDWContext db = new RHDWContext())
             {
-                db.Dispose();
-                MsgBoxHelper.ShowError($"Database.db数据库中不存在表{notExistTableName}");
-                return false;
+                if (File.Exists(db.DbFile))
+                {
+                    try
+                    {
+                        using (FileStream fs = new FileStream(db.DbFile, FileMode.Open))
+                        {
+                        }
+                    }
+                    catch (Exception ex)
+                    {
+                        return $"数据库文件[{db.DbFile}]可能无法写入,请修改其只读属性";
+                    }
+                }
+               
             }
-            var errmsg = db.CheckTableField();
-            if (!string.IsNullOrWhiteSpace(errmsg))
+            using (RHDWLogContext db = new RHDWLogContext())
             {
-                db.Dispose();
-                MsgBoxHelper.ShowError(errmsg);
-                return false;
+                if (File.Exists(db.DbFile))
+                {
+                    try
+                    {
+                        using (FileStream fs = new FileStream(db.DbFile, FileMode.Open))
+                        {
+                        }
+                    }
+                    catch (Exception ex)
+                    {
+                        return $"数据库文件[{db.DbFile}]可能无法写入,请修改其只读属性";
+                    }
+                }
+            }
+            using (RHDWContext db = new RHDWContext())
+            {
+                string notExistTableName = db.CheckTableExist();
+                if (!string.IsNullOrWhiteSpace(notExistTableName))
+                {
+                    db.Dispose();
+                    return $"Database.db数据库中不存在表{notExistTableName}";
+                }
+                return db.CheckTableField();
             }
-            return true;
         }
         private void StartWebApi()
         {

+ 10 - 3
XdCxRhDW.Repostory/EFContext/RHDWContext.cs

@@ -22,9 +22,12 @@ namespace XdCxRhDW.Repostory
 {
     public class RHDWLogContext : DbContext
     {
-
+        public string DbFile;
         public RHDWLogContext() : base("LogDbCon") //配置使用的连接名
         {
+            //|DataDirectory|在mvc等程序中代表了App_Data,在普通程序中代表程序根目录
+            var dbFile = Database.Connection.ConnectionString.Replace("Data Source=", "").Replace("|DataDirectory|\\", "");
+            this.DbFile = dbFile;
         }
 
         protected override void OnModelCreating(DbModelBuilder modelBuilder)
@@ -55,13 +58,17 @@ namespace XdCxRhDW.Repostory
 
             public int pk { get; set; }
         }
-
-        public class DbTableForeignKeyInfo
+        private class DbTableForeignKeyInfo
         {
             public string from { get; set; }
         }
+
+        public string DbFile;
         public RHDWContext() : base("DbCon") //配置使用的连接名
         {
+            //|DataDirectory|在mvc等程序中代表了App_Data,在普通程序中代表程序根目录
+            var dbFile = Database.Connection.ConnectionString.Replace("Data Source=","").Replace("|DataDirectory|\\", "");
+            this.DbFile = dbFile;
         }
 
         public Task<List<T>> SqlQueryAsync<T>(string sql)