|
@@ -5,6 +5,8 @@ using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Data;
|
|
|
using MySql.Data.MySqlClient;
|
|
|
+using parkMonitor.tools;
|
|
|
+using parkMonitor.server.uiLogServer;
|
|
|
|
|
|
namespace parkMonitor.DataBase
|
|
|
{
|
|
@@ -15,8 +17,9 @@ namespace parkMonitor.DataBase
|
|
|
void getUpdate();
|
|
|
void getDelete();
|
|
|
}
|
|
|
- class Operation : IDBOperation
|
|
|
+ public class Operation : IDBOperation
|
|
|
{
|
|
|
+ public static bool malfunction = false;
|
|
|
DBConnection connection = null;
|
|
|
MySqlConnection con = null;
|
|
|
MySqlCommand cmd = null;
|
|
@@ -27,6 +30,7 @@ namespace parkMonitor.DataBase
|
|
|
con = connection.getConn(connectionStr);
|
|
|
cmd = connection.getComm(sql, con);
|
|
|
}
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 数据库查询
|
|
|
/// </summary>
|
|
@@ -34,91 +38,29 @@ namespace parkMonitor.DataBase
|
|
|
/// <returns></returns>
|
|
|
public MySqlDataReader getResultSet(ref int count)
|
|
|
{
|
|
|
- try
|
|
|
- {
|
|
|
- //Console.WriteLine("search " + (con.Ping()).ToString());
|
|
|
- TryOpen("search");
|
|
|
- reader = cmd.ExecuteReader();
|
|
|
- //reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
|
|
- while (reader.Read())
|
|
|
- {
|
|
|
- if (reader.HasRows)
|
|
|
- {
|
|
|
- count++;
|
|
|
- }
|
|
|
- }
|
|
|
- reader.Close();
|
|
|
- reader.Dispose();
|
|
|
- if (count > 0)
|
|
|
- {
|
|
|
- reader = cmd.ExecuteReader();
|
|
|
- }
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- Console.WriteLine("查询数据库异常");
|
|
|
- Console.WriteLine(ex.Message);
|
|
|
- Console.WriteLine(ex.StackTrace);
|
|
|
- DBClose();
|
|
|
- }
|
|
|
+ TryOpen();
|
|
|
+ TryExecute(true);
|
|
|
return reader;
|
|
|
}
|
|
|
|
|
|
public void getInsert()
|
|
|
{
|
|
|
- try
|
|
|
- {
|
|
|
- //Console.WriteLine("insert "+(con.Ping()).ToString());
|
|
|
- //con.Open();
|
|
|
- TryOpen("insert");
|
|
|
- //cmd.ExecuteNonQuery();
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- Console.WriteLine("插入异常");
|
|
|
- Console.WriteLine(ex.Message);
|
|
|
- Console.WriteLine(ex.StackTrace);
|
|
|
- }
|
|
|
- finally
|
|
|
- {
|
|
|
- DBClose();
|
|
|
- }
|
|
|
+ TryOpen();
|
|
|
+ TryExecute(false);
|
|
|
}
|
|
|
|
|
|
public void getUpdate()
|
|
|
{
|
|
|
- try
|
|
|
- {
|
|
|
- TryOpen("update");
|
|
|
- cmd.ExecuteNonQuery();
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- Console.WriteLine("更新异常");
|
|
|
- Console.WriteLine(ex.Message);
|
|
|
- }
|
|
|
- finally
|
|
|
- {
|
|
|
- DBClose();
|
|
|
- }
|
|
|
+ TryOpen();
|
|
|
+ TryExecute(false);
|
|
|
}
|
|
|
|
|
|
public void getDelete()
|
|
|
{
|
|
|
- try
|
|
|
- {
|
|
|
- TryOpen("delete");
|
|
|
- cmd.ExecuteNonQuery();
|
|
|
- }
|
|
|
- catch (Exception)
|
|
|
- {
|
|
|
- Console.WriteLine("删除异常");
|
|
|
- }
|
|
|
- finally
|
|
|
- {
|
|
|
- DBClose();
|
|
|
- }
|
|
|
+ TryOpen();
|
|
|
+ TryExecute(false);
|
|
|
}
|
|
|
+
|
|
|
public int getInsertId()
|
|
|
{
|
|
|
int insertID = 0;
|
|
@@ -126,7 +68,7 @@ namespace parkMonitor.DataBase
|
|
|
{
|
|
|
insertID = Convert.ToInt32(cmd.LastInsertedId);
|
|
|
}
|
|
|
- catch(Exception ex)
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
Console.WriteLine("插入数据库失败");
|
|
|
Console.WriteLine(ex.Message);
|
|
@@ -138,10 +80,20 @@ namespace parkMonitor.DataBase
|
|
|
return insertID;
|
|
|
}
|
|
|
|
|
|
- public void TryOpen(string name)
|
|
|
+ public void TryOpen()
|
|
|
{
|
|
|
+ MyTimer mt = new MyTimer();
|
|
|
+ mt.StartTiming();
|
|
|
+ int count = 0;
|
|
|
while (con.State != ConnectionState.Open)
|
|
|
{
|
|
|
+ mt.EndTiming();
|
|
|
+ if (mt.IsLonger(30, 1, false, out count) && count >= 5)
|
|
|
+ {
|
|
|
+ UILogServer.ins.error("暂时无法连接数据库,暂停处理自动命令,请检查网络连接后点击“启动远端DB”恢复。");
|
|
|
+ Operation.malfunction = true;
|
|
|
+ break;
|
|
|
+ };
|
|
|
try
|
|
|
{
|
|
|
con.Open();
|
|
@@ -150,6 +102,56 @@ namespace parkMonitor.DataBase
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void TryExecute(bool isSearch)
|
|
|
+ {
|
|
|
+ MyTimer mt = new MyTimer();
|
|
|
+ mt.StartTiming();
|
|
|
+ int count = 0;
|
|
|
+ while (true)
|
|
|
+ {
|
|
|
+ mt.EndTiming();
|
|
|
+ if (mt.IsLonger(30, 1, false, out count) && count >= 5)
|
|
|
+ {
|
|
|
+ UILogServer.ins.error("数据库操作异常,暂停处理自动命令。回滚后点击“启动远端DB”恢复");
|
|
|
+ Operation.malfunction = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ try
|
|
|
+ {
|
|
|
+ //非查询,在内部关闭
|
|
|
+ if (!isSearch)
|
|
|
+ {
|
|
|
+ cmd.ExecuteNonQuery();
|
|
|
+ DBClose();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ //查询,在上层关闭
|
|
|
+ else
|
|
|
+ {
|
|
|
+ reader = cmd.ExecuteReader();
|
|
|
+ while (reader.Read())
|
|
|
+ {
|
|
|
+ if (reader.HasRows)
|
|
|
+ {
|
|
|
+ count++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ reader.Close();
|
|
|
+ reader.Dispose();
|
|
|
+ if (count > 0)
|
|
|
+ {
|
|
|
+ reader = cmd.ExecuteReader();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ DBClose();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void DBClose()
|
|
|
{
|
|
|
if (reader != null)
|