Browse Source

Merge remote-tracking branch 'jinwang/dev' into dev

# Conflicts:
#	parkMonitor/Database2/ConnectionPoolManager.cs
#	parkMonitor/server/CoreThread/AbstractCmd.cs
yc_t 6 years ago
parent
commit
fc8dd57ce5

+ 58 - 24
parkMonitor/Database2/AbstractCommand.cs

@@ -11,6 +11,7 @@ namespace parkMonitor.Database2
     abstract class AbstractCommand
     {
         public string sql { set; get; }
+        public List<string> strs { set; get; }
 
         public Connection conn { set; get; }
 
@@ -25,16 +26,37 @@ namespace parkMonitor.Database2
             this.sql = sql;
             this.conn = conn;
         }
+        public AbstractCommand(Connection conn, List<string> strs)
+        {
+            this.conn = conn;
+            this.strs = strs;
+        }
 
         protected MySqlCommand CreateCommand()
         {
-            MySqlCommand cmd = new MySqlCommand(sql, conn.mySqlConnection);
-
+            MySqlCommand cmd = null;
+            try
+            {
+                cmd = new MySqlCommand(sql, conn.mySqlConnection);
+            }
+            catch (Exception ex) {
+                Console.WriteLine(ex.Message);
+                Console.WriteLine("MySqlCommand异常");
+            }
             return cmd;
         }
         protected MySqlCommand CreateCommand(string sql, MySqlConnection mySqlConnection)
         {
-            MySqlCommand cmd = new MySqlCommand(sql, conn.mySqlConnection);
+            MySqlCommand cmd = null;
+            try
+            {
+                cmd = new MySqlCommand(sql, conn.mySqlConnection);
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.Message);
+                Console.WriteLine("MySqlCommand异常");
+            }
             return cmd;
         }
         /// <summary>
@@ -53,8 +75,16 @@ namespace parkMonitor.Database2
         public override object ExecuteSqlCommand()
         {
             object result = null;
-            MySqlCommand cmd = CreateCommand(sql, conn.mySqlConnection);
-            result = cmd.ExecuteReader();
+            try
+            {
+                MySqlCommand cmd = CreateCommand(sql, conn.mySqlConnection);
+                result = cmd.ExecuteReader();
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.Message);
+                Console.WriteLine("ExecuteReader执行异常");
+            }
             return result;
         }
     }
@@ -67,10 +97,17 @@ namespace parkMonitor.Database2
         public override object ExecuteSqlCommand()
         {
             object result = null;
-            MySqlCommand cmd = CreateCommand(sql, conn.mySqlConnection);
-            result = cmd.ExecuteNonQuery();
+            try
+            {
+                MySqlCommand cmd = CreateCommand(sql, conn.mySqlConnection);
+                result = cmd.ExecuteNonQuery();
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.Message);
+                Console.WriteLine("ExecuteNonQuery执行异常");
+            }
             return result;
-
         }
     }
     /// <summary>
@@ -81,8 +118,16 @@ namespace parkMonitor.Database2
         public override object ExecuteSqlCommand()
         {
             object result = null;
-            MySqlCommand cmd = CreateCommand(sql, conn.mySqlConnection);
-            result = cmd.ExecuteScalar();
+            try
+            {
+                MySqlCommand cmd = CreateCommand(sql, conn.mySqlConnection);
+                result = cmd.ExecuteScalar();
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.Message);
+                Console.WriteLine("ExecuteScalar执行异常");
+            }
             return result;
         }
     }
@@ -94,7 +139,6 @@ namespace parkMonitor.Database2
         public override object ExecuteSqlCommand()
         {
             object result = null;
-
             MySqlCommand cmd = CreateCommand(sql, conn.mySqlConnection);
             cmd.CommandType = CommandType.StoredProcedure;
             //cmd.Parameters.AddWithValue();
@@ -116,7 +160,7 @@ namespace parkMonitor.Database2
             strs = null;
         }
         public TransactionCommand(string sql, Connection conn, List<string> strs)
-            : base(sql, conn)
+            : base(conn,strs)
         {
             this.strs = strs;
         }
@@ -137,31 +181,21 @@ namespace parkMonitor.Database2
                     result = Convert.ToInt32(myCommand.LastInsertedId);
                 }
                 myTrans.Commit();
-                //result = 1;
-                Console.WriteLine("Both records are written to database.");
             }
             catch (Exception e)
             {
                 try
                 {
                     myTrans.Rollback();
-                    //result = 2;
                 }
                 catch (MySqlException ex)
                 {
                     if (myTrans.Connection != null)
                     {
-                        Console.WriteLine("An exception of type " + ex.GetType() +
-                        " was encountered while attempting to roll back the transaction.");
-                        Console.WriteLine("事务回滚失败");
+                        Console.WriteLine(ex.Message + "事务回滚失败");
                     }
-                    //result = 3;
                 }
-
-                Console.WriteLine("An exception of type " + e.GetType() +
-                " was encountered while inserting the data.");
-                Console.WriteLine("Neither record was written to database.");
-                Console.WriteLine("事务操作失败");
+                Console.WriteLine(e.Message + "事务操作失败");
             }
             finally
             {

+ 32 - 18
parkMonitor/Database2/ConnectionPoolManager.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 using System.Collections.Concurrent;
 using parkMonitor.tools;
@@ -81,6 +82,7 @@ namespace parkMonitor.Database2
         /// 定时重置连接
         /// </summary>
         /// <param name="max"></param>
+        /// <param name="connectionString"></param>
         /// <param name="bq"></param>
         public static void ResetConnPooling(int max, string connectionString, BlockingQueue bq)
         {
@@ -106,25 +108,34 @@ namespace parkMonitor.Database2
         {
             Connection conn = null;
             int total = bq.Count();
-            for (int i = 0; i < total; i++)
+            if (total == 0)
+            {
+                Console.WriteLine("请工作人员检查数据库是否开启,网络是否通畅!等待50秒");
+                Thread.Sleep(5000);
+                //在此可以切换数据库
+            }
+            else
             {
-                conn = GetConnection(bq);
-                try
+                for (int i = 0; i < total; i++)
                 {
-                    string sql = "select count(1) from garage";
-                    MySqlCommand cmd = new MySqlCommand(sql, conn.mySqlConnection);
-                    object result = cmd.ExecuteScalar();
-                    if (result != null)
+                    conn = GetConnection(bq);
+                    try
                     {
-                        int r = Convert.ToInt32(result);
-                        //Console.WriteLine("Number of garage in the database is: " + r);
+                        string sql = "select count(1) from garage";
+                        DBAccess access = new DBAccess(sql, conn, CommandTypes.SCALARCOMMAND);
+                        object result = access.GetResult();
+                        if (result != null)
+                        {
+                            int r = Convert.ToInt32(result);
+                            Console.WriteLine("连接测试正常");
+                        }
+                        ReleaseConnection(conn, bq);
+                    }
+                    catch (Exception ex)
+                    {
+                        conn.Dispose();
+                        Console.WriteLine(ex.Message);
                     }
-                    ReleaseConnection(conn,bq);
-                }
-                catch (Exception ex)
-                {
-                    conn.Dispose();
-                    Console.WriteLine(ex.Message);
                 }
             }
             if (bq.Count() < min)
@@ -146,10 +157,13 @@ namespace parkMonitor.Database2
         /// </summary>
         public static void CloseAll(BlockingQueue bq)
         {
-            while (bq.Count() > 0)
+            if (bq.Count() > 0)
             {
-                Connection connection = (Connection)bq.Dequeue();
-                connection.Close();
+                while (bq.Count() > 0)
+                {
+                    Connection connection = (Connection)bq.Dequeue();
+                    connection.Close();
+                }
             }
             Console.WriteLine("所有连接成功关闭!");
         }

+ 10 - 0
parkMonitor/Database2/DBAccess.cs

@@ -54,5 +54,15 @@ namespace parkMonitor.Database2
             result = abscommand.ExecuteSqlCommand();
             return result;
         }
+
+        public object DealTransaction()
+        {
+            object result = null;
+            AbstractCommand abscommand = SimpleCommandFactory.CreateCommandAndExcute(commType);
+            abscommand.strs = strs;
+            abscommand.conn = conn;
+            result = abscommand.ExecuteSqlCommand();
+            return result;
+        }
     }
 }

+ 342 - 139
parkMonitor/Database2/DBOperation.cs

@@ -25,19 +25,35 @@ namespace parkMonitor.Database2
             MySqlDataReader reader = null;
             int garageFreeSpace = 0;
             string sql = "select * from garage where garageID = '" + garageID + "'";
-            Operation oper = new Operation(bq, sql);
-            int count = 0;
-            reader = oper.getResultSet(ref count);
-            if (count > 0 && reader.Read())
+            Connection conn = ConnectionPoolManager.GetConnection(bq);
+            DBAccess access = new DBAccess(sql, conn, CommandTypes.QUERYCOMMAND);
+            object result = access.GetResult();
+            try
             {
-                garageFreeSpace = reader.GetInt32("garageFreeSpace");
-                return garageFreeSpace;
+                reader = (MySqlDataReader)result;
+                while (reader.Read())
+                {
+                    if (reader.HasRows)
+                    {
+                        garageFreeSpace = reader.GetInt32("garageFreeSpace");
+                        return garageFreeSpace;
+                    }
+                    else
+                    {
+                        Console.WriteLine("车位剩余数查无结果");
+                    }
+                }               
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.Message+"查询剩余车位异常");
             }
-            else
+            finally
             {
-                Console.WriteLine("车位剩余数查无结果");
+                reader.Close();
+                reader.Dispose();
+                ConnectionPoolManager.ReleaseConnection(conn, bq);
             }
-            oper.DBClose(null,null,reader);
             return 0;
         }
         /// <summary>
@@ -51,29 +67,38 @@ namespace parkMonitor.Database2
             Dictionary<int, Parking_Space> lps = new Dictionary<int, Parking_Space>();
             MySqlDataReader reader = null;
             string sql = "select * from parkingspace where garageID = '" + garageID + "' ";
-            Operation oper = new Operation(bq, sql);
-            int count = 0;
-            reader = oper.getResultSet(ref count);
-            if (count > 0)
+            Connection conn = ConnectionPoolManager.GetConnection(bq);
+            DBAccess access = new DBAccess(sql, conn, CommandTypes.QUERYCOMMAND);
+            object result = access.GetResult();
+            try
             {
-                for (int i = 1; i <= count && reader.Read(); i++)
+                reader = (MySqlDataReader)result;
+                while (reader.Read())
                 {
-                    Parking_Space ps = new Parking_Space();
-                    ps.parkingSpaceID = reader.GetInt32("parkingSpaceID");
-                    ps.parkingSpaceX = reader.GetInt32("parkingSpaceX");
-                    ps.parkingSpaceY = reader.GetInt32("parkingSpaceY");
-                    ps.parkingSpaceZ = reader.GetInt32("parkingSpaceZ");
-                    ps.parkingSpaceState = reader.GetInt32("parkingSpaceState");
-                    ps.garageID = garageID;
-                    lps.Add(ps.parkingSpaceID, (Parking_Space)ps.Clone());
+                    if (reader.HasRows)
+                    {
+                        Parking_Space ps = new Parking_Space();
+                        ps.parkingSpaceID = reader.GetInt32("parkingSpaceID");
+                        ps.parkingSpaceX = reader.GetInt32("parkingSpaceX");
+                        ps.parkingSpaceY = reader.GetInt32("parkingSpaceY");
+                        ps.parkingSpaceZ = reader.GetInt32("parkingSpaceZ");
+                        ps.parkingSpaceState = reader.GetInt32("parkingSpaceState");
+                        ps.garageID = garageID;
+                        lps.Add(ps.parkingSpaceID, (Parking_Space)ps.Clone());
+                    }
                 }
                 return lps;
             }
-            else
+            catch (Exception ex)
             {
-                Console.WriteLine("所有车位查无结果");
+                Console.WriteLine(ex.Message + "查询车位表异常");
+            }
+            finally
+            {
+                reader.Close();
+                reader.Dispose();
+                ConnectionPoolManager.ReleaseConnection(conn, bq);
             }
-            oper.DBClose(null,null,reader);
             return null;
         }
         /// <summary>
@@ -87,27 +112,39 @@ namespace parkMonitor.Database2
             Vehicle v = new Vehicle();
             MySqlDataReader reader = null;
             string sql = "select * from vehicle where numberPlate = '" + numberPlate + "'";
-            Operation oper = new Operation(bq, sql);
-            int count = 0;
-            reader = oper.getResultSet(ref count);
-            if (count > 0 && reader.Read())
-            {
-                int parkingSpaceID = reader.GetInt32("parkingSpaceID");
-                int garageID = reader.GetInt32("garageID");
-                int frontwheelbase = reader.GetInt32("frontwheelbase");
-                int rearwheelbase = reader.GetInt32("rearwheelbase");
-                v.parkingRecordsID = reader.GetInt32("parkingRecordsID");
-                v.parkingSpaceID = parkingSpaceID;
-                v.garageID = garageID;
-                v.frontwheelbase = frontwheelbase;
-                v.rearwheelbase = rearwheelbase;
-                return v;
-            }
-            else
-            {
-                Console.WriteLine("云端车辆查无结果");
-            }
-            oper.DBClose(null,null,reader);
+            Connection conn = ConnectionPoolManager.GetConnection(bq);
+            DBAccess access = new DBAccess(sql, conn, CommandTypes.QUERYCOMMAND);
+            object result = access.GetResult();
+            try
+            {
+                reader = (MySqlDataReader)result;
+                while (reader.Read())
+                {
+                    if (reader.HasRows)
+                    {
+                        int parkingSpaceID = reader.GetInt32("parkingSpaceID");
+                        int garageID = reader.GetInt32("garageID");
+                        int frontwheelbase = reader.GetInt32("frontwheelbase");
+                        int rearwheelbase = reader.GetInt32("rearwheelbase");
+                        v.parkingRecordsID = reader.GetInt32("parkingRecordsID");
+                        v.parkingSpaceID = parkingSpaceID;
+                        v.garageID = garageID;
+                        v.frontwheelbase = frontwheelbase;
+                        v.rearwheelbase = rearwheelbase;
+                        return v;
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.Message + "查询车辆表异常");
+            }
+            finally
+            {
+                reader.Close();
+                reader.Dispose();
+                ConnectionPoolManager.ReleaseConnection(conn, bq);
+            }
             return null;
         }
         /// <summary>
@@ -121,22 +158,34 @@ namespace parkMonitor.Database2
             Parking_Space ps = new Parking_Space();
             MySqlDataReader reader = null;
             string sql = "select * from parkingspace where parkingSpaceID = '" + parkingSpaceID + " '";
-            Operation oper = new Operation(bq, sql);
-            int count = 0;
-            reader = oper.getResultSet(ref count);
-            if (count > 0 && reader.Read())
+            Connection conn = ConnectionPoolManager.GetConnection(bq);
+            DBAccess access = new DBAccess(sql, conn, CommandTypes.QUERYCOMMAND);
+            object result = access.GetResult();
+            try
             {
-                ps.parkingSpaceID = parkingSpaceID;
-                ps.parkingSpaceX = reader.GetInt32("parkingSpaceX");
-                ps.parkingSpaceY = reader.GetInt32("parkingSpaceY");
-                ps.parkingSpaceZ = reader.GetInt32("parkingSpaceZ");
-                return ps;
+                reader = (MySqlDataReader)result;
+                while (reader.Read())
+                {
+                    if (reader.HasRows)
+                    {
+                        ps.parkingSpaceID = parkingSpaceID;
+                        ps.parkingSpaceX = reader.GetInt32("parkingSpaceX");
+                        ps.parkingSpaceY = reader.GetInt32("parkingSpaceY");
+                        ps.parkingSpaceZ = reader.GetInt32("parkingSpaceZ");
+                        return ps;
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.Message + "查询车位表获取x,y,z异常");
             }
-            else
+            finally
             {
-                Console.WriteLine("车位xyz查无结果");
+                reader.Close();
+                reader.Dispose();
+                ConnectionPoolManager.ReleaseConnection(conn, bq);
             }
-            oper.DBClose(null,null,reader);
             return null;
         }
         /// <summary>
@@ -148,63 +197,31 @@ namespace parkMonitor.Database2
         public bool IsTelRegister(BlockingQueue bq, string tel)
         {
             bool isTelRegister = false;
-            MySqlDataReader reader = null;
             string sql = "select * from user where userTelephone = '" + tel + "'";
-            Operation oper = new Operation(bq, sql);
-            int count = 0;
-            reader = oper.getResultSet(ref count);
-            if (count > 0)
-            {
-                isTelRegister = true;
-            }
-            else
+            Connection conn = ConnectionPoolManager.GetConnection(bq);
+            DBAccess access = new DBAccess(sql, conn, CommandTypes.SCALARCOMMAND);
+            object result = access.GetResult();
+            try
             {
-                isTelRegister = false;
+                int count = (int)result;
+                if (count > 0)
+                {
+                    isTelRegister = true;
+                }
+                else
+                {
+                    isTelRegister = false;
+                }
             }
-            oper.DBClose(null,null,reader);
-            return isTelRegister;
-        }
-        /// <summary>
-        /// 注册信息写入数据库,返回注册成功信息
-        /// </summary>
-        /// <param name="bq"></param>
-        /// <param name="tel"></param>
-        /// <param name="password"></param>
-        /// <returns></returns>
-        public int InsertUser(BlockingQueue bq, string tel, string password)
-        {
-            string sql = "insert into user(userTelephone,userPassword,userLevel) values('" + tel + "','" + password + "',1)";
-            int userID = 0;
-            Operation oper = new Operation(bq, sql);
-            oper.getInsert();
-            userID = oper.getInsertId();
-            return userID;
-        }
-        /// <summary>
-        /// 根据电话号码查询userID
-        /// </summary>
-        /// <param name="bq"></param>
-        /// <param name="tel"></param>
-        /// <returns></returns>
-        public int GetUserID(BlockingQueue bq, string tel)
-        {
-            int userID = 0;
-            MySqlDataReader reader = null;
-            string sql = "select userID from user where userTelephone = '" + tel + "'";
-            int count = 0;
-            Operation oper = new Operation(bq, sql);
-            reader = oper.getResultSet(ref count);
-            if (count > 0 && reader.Read())
+            catch (Exception ex)
             {
-                userID = reader.GetInt32("userID");
-                return userID;
+                Console.WriteLine(ex.Message + "检测电话号码注册异常");
             }
-            else
+            finally
             {
-                Console.WriteLine("userID查无结果");
+                ConnectionPoolManager.ReleaseConnection(conn, bq);
             }
-            oper.DBClose(null,null,reader);
-            return 0;
+            return isTelRegister;
         }
         /// <summary>
         /// 查询停车记录id
@@ -217,19 +234,35 @@ namespace parkMonitor.Database2
             int parkingRecordsID = 0;
             MySqlDataReader reader = null;
             string sql = "select parkingRecordsID from parkingrecords where numberPlate = '" + numberPlate + "' and parkingRecordsState = 3";
-            int count = 0;
-            Operation oper = new Operation(bq, sql);
-            reader = oper.getResultSet(ref count);
-            if (count > 0 && reader.Read())
+            Connection conn = ConnectionPoolManager.GetConnection(bq);
+            DBAccess access = new DBAccess(sql, conn, CommandTypes.QUERYCOMMAND);
+            object result = access.GetResult();
+            try
+            {
+                reader = (MySqlDataReader)result;
+                while (reader.Read())
+                {
+                    if (reader.HasRows)
+                    {
+                        parkingRecordsID = reader.GetInt32("parkingRecordsID");
+                        return parkingRecordsID;
+                    }
+                    else
+                    {
+                        Console.WriteLine("停车记录id查无结果");
+                    }
+                }
+            }
+            catch (Exception ex)
             {
-                parkingRecordsID = reader.GetInt32("parkingRecordsID");
-                return parkingRecordsID;
+                Console.WriteLine(ex.Message + "查询停车记录id异常");
             }
-            else
+            finally
             {
-                Console.WriteLine("停车记录id查无结果");
+                reader.Close();
+                reader.Dispose();
+                ConnectionPoolManager.ReleaseConnection(conn, bq);
             }
-            oper.DBClose(null,null,reader);
             return 0;
         }
 
@@ -243,20 +276,30 @@ namespace parkMonitor.Database2
         public bool IsNumberPlate(BlockingQueue bq, string numberPlate, int garageID)
         {
             bool isNumberPlate = true;
-            MySqlDataReader reader = null;
             string sql = "select * from parkingrecords where numberPlate = '" + numberPlate + "' and parkingRecordsState = 3 and garageID = '" + garageID + "'";
-            Operation oper = new Operation(bq, sql);
-            int count = 0;
-            reader = oper.getResultSet(ref count);
-            if (count > 0 && reader.Read())
+            Connection conn = ConnectionPoolManager.GetConnection(bq);
+            DBAccess access = new DBAccess(sql, conn, CommandTypes.SCALARCOMMAND);
+            object result = access.GetResult();
+            try
             {
-                isNumberPlate = true;
+                int count = (int)result;
+                if (count > 0)
+                {
+                    isNumberPlate = true;
+                }
+                else
+                {
+                    isNumberPlate = false;
+                }
             }
-            else
+            catch (Exception ex)
             {
-                isNumberPlate = false;
+                Console.WriteLine(ex.Message + "检测车库中车辆异常");
+            }
+            finally
+            {
+                ConnectionPoolManager.ReleaseConnection(conn, bq);
             }
-            oper.DBClose(null,null,reader);
             return isNumberPlate;
         }
         /// <summary>
@@ -269,19 +312,29 @@ namespace parkMonitor.Database2
         {
             bool isNumberPlateFromVehicle = true;
             string sql = "select * from vehicle where numberPlate = '" + numberPlate + "' and vehiclepParkState = 1";
-            MySqlDataReader reader = null;
-            Operation oper = new Operation(bq, sql);
-            int count = 0;
-            reader = oper.getResultSet(ref count);
-            if (count > 0 && reader.Read())
+            Connection conn = ConnectionPoolManager.GetConnection(bq);
+            DBAccess access = new DBAccess(sql, conn, CommandTypes.SCALARCOMMAND);
+            object result = access.GetResult();
+            try
             {
-                isNumberPlateFromVehicle = true;
+                int count = (int)result;
+                if (count > 0)
+                {
+                    isNumberPlateFromVehicle = true;
+                }
+                else
+                {
+                    isNumberPlateFromVehicle = false;
+                }
             }
-            else
+            catch (Exception ex)
             {
-                isNumberPlateFromVehicle = false;
+                Console.WriteLine(ex.Message + "检测车辆表车辆号牌异常");
+            }
+            finally
+            {
+                ConnectionPoolManager.ReleaseConnection(conn, bq);
             }
-            oper.DBClose(null,null,reader);
             return isNumberPlateFromVehicle;
         }
         /// <summary>
@@ -293,8 +346,10 @@ namespace parkMonitor.Database2
         public void UpdateVehicleParkState(BlockingQueue bq, string numberPlate, int vehiclepParkState)
         {
             string sql = "update vehicle set vehiclepParkState = '" + vehiclepParkState + "'where numberPlate = '" + numberPlate + "'";
-            Operation oper = new Operation(bq, sql);
-            oper.getUpdate();
+            Connection conn = ConnectionPoolManager.GetConnection(bq);
+            DBAccess access = new DBAccess(sql, conn, CommandTypes.NOQUERYCOMMAND);
+            object result = access.GetResult();
+            ConnectionPoolManager.ReleaseConnection(conn, bq);
         }
         /// <summary>
         /// 更新车位状态
@@ -305,8 +360,156 @@ namespace parkMonitor.Database2
         public void UpdateParkingSpaceState(BlockingQueue bq, int parkingSpaceID, int parkingSpaceState)
         {
             string sql = "update parkingspace set parkingSpaceState = '" + parkingSpaceState + "'where parkingSpaceID = '" + parkingSpaceID + "'";
-            Operation oper = new Operation(bq, sql);
-            oper.getUpdate();
+            Connection conn = ConnectionPoolManager.GetConnection(bq);
+            DBAccess access = new DBAccess(sql, conn, CommandTypes.NOQUERYCOMMAND);
+            object result = access.GetResult(); 
+            ConnectionPoolManager.ReleaseConnection(conn, bq);
+        }
+        ///// <summary>
+        ///// 插入停车记录表事务
+        ///// </summary>
+        ///// <param name="bq"></param>
+        ///// <param name="strs"></param>
+        ///// <returns></returns>
+        //public static int InsertParkingRecords(BlockingQueue bq, List<string> strs)
+        //{
+        //    Connection conn = ConnectionPoolManager.GetConnection(bq);
+        //    DBAccess access = new DBAccess(conn, CommandTypes.TRANSACTIONCOMMAND, strs);
+        //    object result = access.DealTransaction();
+        //    int parkingRecordsID = 0;
+        //    try
+        //    {
+        //        parkingRecordsID = (int)result;
+        //    }
+        //    catch (Exception ex)
+        //    {
+        //        Console.WriteLine(ex.Message+"停车记录ID获取失败");
+        //    }
+        //    finally
+        //    {
+        //        ConnectionPoolManager.ReleaseConnection(conn, bq);
+        //    }
+        //    return parkingRecordsID;
+        //}
+        ///// <summary>
+        ///// 更新数据库事务
+        ///// </summary>
+        ///// <param name="bq"></param>
+        ///// <param name="strs"></param>
+        //public static void UpdateTransaction(BlockingQueue bq, List<string> strs)
+        //{
+        //    Connection conn = ConnectionPoolManager.GetConnection(bq);
+        //    DBAccess access = new DBAccess(conn, CommandTypes.TRANSACTIONCOMMAND, strs);
+        //    object result = access.DealTransaction();
+        //}
+
+        /// <summary>
+        /// 插入停车记录表事务
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="strs"></param>
+        /// <returns></returns>
+        public static int InsertParkingRecords(BlockingQueue bq, List<string> strs)
+        {
+            Connection conn = ConnectionPoolManager.GetConnection(bq);
+            object result = null;
+            MySqlCommand myCommand = conn.mySqlConnection.CreateCommand();
+            MySqlTransaction myTrans;
+            myTrans = conn.mySqlConnection.BeginTransaction();
+            myCommand.Connection = conn.mySqlConnection;
+            myCommand.Transaction = myTrans;
+            try
+            {
+                for (int i = 0; i < strs.Count(); i++)
+                {
+                    myCommand.CommandText = strs[i];
+                    myCommand.ExecuteNonQuery();
+                    result = Convert.ToInt32(myCommand.LastInsertedId);
+                }
+                myTrans.Commit();
+            }
+            catch (Exception e)
+            {
+                try
+                {
+                    myTrans.Rollback();
+                }
+                catch (MySqlException ex)
+                {
+                    if (myTrans.Connection != null)
+                    {
+                        Console.WriteLine(ex.Message + "事务回滚失败");
+                    }
+                }
+                Console.WriteLine(e.Message + "事务操作失败");
+            }
+            finally
+            {
+                myCommand.Dispose();
+                myTrans.Dispose();
+                //ConnectionPoolManager.ReleaseConnection(conn);
+            }
+            int parkingRecordsID = 0;
+            try
+            {
+                parkingRecordsID = (int)result;
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.Message + "停车记录ID获取失败");
+            }
+            finally
+            {
+                ConnectionPoolManager.ReleaseConnection(conn, bq);
+            }
+            return parkingRecordsID;
+        }
+        /// <summary>
+        /// 更新数据库事务
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="strs"></param>
+        public static object UpdateTransaction(BlockingQueue bq, List<string> strs)
+        {
+            Connection conn = ConnectionPoolManager.GetConnection(bq);
+            object result = false;
+            MySqlCommand myCommand = conn.mySqlConnection.CreateCommand();
+            MySqlTransaction myTrans;
+            myTrans = conn.mySqlConnection.BeginTransaction();
+            myCommand.Connection = conn.mySqlConnection;
+            myCommand.Transaction = myTrans;
+            try
+            {
+                for (int i = 0; i < strs.Count(); i++)
+                {
+                    myCommand.CommandText = strs[i];
+                    myCommand.ExecuteNonQuery();
+                    result = true;
+                }
+                myTrans.Commit();
+            }
+            catch (Exception e)
+            {
+                try
+                {
+                    myTrans.Rollback();
+                }
+                catch (MySqlException ex)
+                {
+                    if (myTrans.Connection != null)
+                    {
+                        Console.WriteLine(ex.Message + "事务回滚失败");
+                    }
+                }
+                Console.WriteLine(e.Message + "事务操作失败");
+            }
+            finally
+            {
+                myCommand.Dispose();
+                myTrans.Dispose();
+                ConnectionPoolManager.ReleaseConnection(conn,bq);
+            }
+            return result;
         }
     }
 }

+ 309 - 0
parkMonitor/Database2/DBOperation2.cs

@@ -0,0 +1,309 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using MySql.Data.MySqlClient;
+using parkMonitor.tools;
+using parkMonitor.server.CoreThread;
+
+namespace parkMonitor.Database2
+{
+    class DBOperation2
+    {
+        /// <summary>
+        /// 查询车库表获得剩余车位数
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="garageID"></param>
+        /// <returns></returns>
+        public int getGarageFreeSpace(BlockingQueue bq, int garageID)
+        {
+            MySqlDataReader reader = null;
+            int garageFreeSpace = 0;
+            string sql = "select * from garage where garageID = '" + garageID + "'";
+            Operation oper = new Operation(bq, sql);
+            int count = 0;
+            reader = oper.getResultSet(ref count);
+            if (count > 0 && reader.Read())
+            {
+                garageFreeSpace = reader.GetInt32("garageFreeSpace");
+                return garageFreeSpace;
+            }
+            else
+            {
+                Console.WriteLine("车位剩余数查无结果");
+            }
+            oper.DBClose(null, null, reader);
+            return 0;
+        }
+        /// <summary>
+        /// 查询所有车位位置及状态
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="garageID"></param>
+        /// <returns></returns>
+        public Dictionary<int, Parking_Space> GetAllParkingSpace(BlockingQueue bq, int garageID)
+        {
+            Dictionary<int, Parking_Space> lps = new Dictionary<int, Parking_Space>();
+            MySqlDataReader reader = null;
+            string sql = "select * from parkingspace where garageID = '" + garageID + "' ";
+            Operation oper = new Operation(bq, sql);
+            int count = 0;
+            reader = oper.getResultSet(ref count);
+            if (count > 0)
+            {
+                for (int i = 1; i <= count && reader.Read(); i++)
+                {
+                    Parking_Space ps = new Parking_Space();
+                    ps.parkingSpaceID = reader.GetInt32("parkingSpaceID");
+                    ps.parkingSpaceX = reader.GetInt32("parkingSpaceX");
+                    ps.parkingSpaceY = reader.GetInt32("parkingSpaceY");
+                    ps.parkingSpaceZ = reader.GetInt32("parkingSpaceZ");
+                    ps.parkingSpaceState = reader.GetInt32("parkingSpaceState");
+                    ps.garageID = garageID;
+                    lps.Add(ps.parkingSpaceID, (Parking_Space)ps.Clone());
+                }
+                return lps;
+            }
+            else
+            {
+                Console.WriteLine("所有车位查无结果");
+            }
+            oper.DBClose(null, null, reader);
+            return null;
+        }
+        /// <summary>
+        /// 根据车牌查询得到车库id和车位id以及轮距
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="numberPlate"></param>
+        /// <returns></returns>
+        public Vehicle GetVehicle(BlockingQueue bq, string numberPlate)
+        {
+            Vehicle v = new Vehicle();
+            MySqlDataReader reader = null;
+            string sql = "select * from vehicle where numberPlate = '" + numberPlate + "'";
+            Operation oper = new Operation(bq, sql);
+            int count = 0;
+            reader = oper.getResultSet(ref count);
+            if (count > 0 && reader.Read())
+            {
+                int parkingSpaceID = reader.GetInt32("parkingSpaceID");
+                int garageID = reader.GetInt32("garageID");
+                int frontwheelbase = reader.GetInt32("frontwheelbase");
+                int rearwheelbase = reader.GetInt32("rearwheelbase");
+                v.parkingRecordsID = reader.GetInt32("parkingRecordsID");
+                v.parkingSpaceID = parkingSpaceID;
+                v.garageID = garageID;
+                v.frontwheelbase = frontwheelbase;
+                v.rearwheelbase = rearwheelbase;
+                return v;
+            }
+            else
+            {
+                Console.WriteLine("云端车辆查无结果");
+            }
+            oper.DBClose(null, null, reader);
+            return null;
+        }
+        /// <summary>
+        /// 根据车位id获得x,y,z
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="parkingSpaceID"></param>
+        /// <returns></returns>
+        public Parking_Space GetFetchingSpace(BlockingQueue bq, int parkingSpaceID)
+        {
+            Parking_Space ps = new Parking_Space();
+            MySqlDataReader reader = null;
+            string sql = "select * from parkingspace where parkingSpaceID = '" + parkingSpaceID + " '";
+            Operation oper = new Operation(bq, sql);
+            int count = 0;
+            reader = oper.getResultSet(ref count);
+            if (count > 0 && reader.Read())
+            {
+                ps.parkingSpaceID = parkingSpaceID;
+                ps.parkingSpaceX = reader.GetInt32("parkingSpaceX");
+                ps.parkingSpaceY = reader.GetInt32("parkingSpaceY");
+                ps.parkingSpaceZ = reader.GetInt32("parkingSpaceZ");
+                return ps;
+            }
+            else
+            {
+                Console.WriteLine("车位xyz查无结果");
+            }
+            oper.DBClose(null, null, reader);
+            return null;
+        }
+        /// <summary>
+        /// 查询手机号是否被注册
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="tel"></param>
+        /// <returns></returns>
+        public bool IsTelRegister(BlockingQueue bq, string tel)
+        {
+            bool isTelRegister = false;
+            MySqlDataReader reader = null;
+            string sql = "select * from user where userTelephone = '" + tel + "'";
+            Operation oper = new Operation(bq, sql);
+            int count = 0;
+            reader = oper.getResultSet(ref count);
+            if (count > 0)
+            {
+                isTelRegister = true;
+            }
+            else
+            {
+                isTelRegister = false;
+            }
+            oper.DBClose(null, null, reader);
+            return isTelRegister;
+        }
+        /// <summary>
+        /// 注册信息写入数据库,返回注册成功信息
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="tel"></param>
+        /// <param name="password"></param>
+        /// <returns></returns>
+        public int InsertUser(BlockingQueue bq, string tel, string password)
+        {
+            string sql = "insert into user(userTelephone,userPassword,userLevel) values('" + tel + "','" + password + "',1)";
+            int userID = 0;
+            Operation oper = new Operation(bq, sql);
+            oper.getInsert();
+            userID = oper.getInsertId();
+            return userID;
+        }
+        /// <summary>
+        /// 根据电话号码查询userID
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="tel"></param>
+        /// <returns></returns>
+        public int GetUserID(BlockingQueue bq, string tel)
+        {
+            int userID = 0;
+            MySqlDataReader reader = null;
+            string sql = "select userID from user where userTelephone = '" + tel + "'";
+            int count = 0;
+            Operation oper = new Operation(bq, sql);
+            reader = oper.getResultSet(ref count);
+            if (count > 0 && reader.Read())
+            {
+                userID = reader.GetInt32("userID");
+                return userID;
+            }
+            else
+            {
+                Console.WriteLine("userID查无结果");
+            }
+            oper.DBClose(null, null, reader);
+            return 0;
+        }
+        /// <summary>
+        /// 查询停车记录id
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="numberPlate"></param>
+        /// <returns></returns>
+        public int GetParkingRecordsID(BlockingQueue bq, string numberPlate)
+        {
+            int parkingRecordsID = 0;
+            MySqlDataReader reader = null;
+            string sql = "select parkingRecordsID from parkingrecords where numberPlate = '" + numberPlate + "' and parkingRecordsState = 3";
+            int count = 0;
+            Operation oper = new Operation(bq, sql);
+            reader = oper.getResultSet(ref count);
+            if (count > 0 && reader.Read())
+            {
+                parkingRecordsID = reader.GetInt32("parkingRecordsID");
+                return parkingRecordsID;
+            }
+            else
+            {
+                Console.WriteLine("停车记录id查无结果");
+            }
+            oper.DBClose(null, null, reader);
+            return 0;
+        }
+
+        /// <summary>
+        /// 车库有无此车
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="numberPlate"></param>
+        /// <param name="garageID"></param>
+        /// <returns></returns>
+        public bool IsNumberPlate(BlockingQueue bq, string numberPlate, int garageID)
+        {
+            bool isNumberPlate = true;
+            MySqlDataReader reader = null;
+            string sql = "select * from parkingrecords where numberPlate = '" + numberPlate + "' and parkingRecordsState = 3 and garageID = '" + garageID + "'";
+            Operation oper = new Operation(bq, sql);
+            int count = 0;
+            reader = oper.getResultSet(ref count);
+            if (count > 0 && reader.Read())
+            {
+                isNumberPlate = true;
+            }
+            else
+            {
+                isNumberPlate = false;
+            }
+            oper.DBClose(null, null, reader);
+            return isNumberPlate;
+        }
+        /// <summary>
+        /// 判断车辆表中是否存在该车辆号牌
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="numberPlate"></param>
+        /// <returns></returns>
+        public bool IsNumberPlateFromVehicle(BlockingQueue bq, string numberPlate)
+        {
+            bool isNumberPlateFromVehicle = true;
+            string sql = "select * from vehicle where numberPlate = '" + numberPlate + "' and vehiclepParkState = 1";
+            MySqlDataReader reader = null;
+            Operation oper = new Operation(bq, sql);
+            int count = 0;
+            reader = oper.getResultSet(ref count);
+            if (count > 0 && reader.Read())
+            {
+                isNumberPlateFromVehicle = true;
+            }
+            else
+            {
+                isNumberPlateFromVehicle = false;
+            }
+            oper.DBClose(null, null, reader);
+            return isNumberPlateFromVehicle;
+        }
+        /// <summary>
+        /// 根据车牌号更新车辆状态
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="numberPlate"></param>
+        /// <param name="vehiclepParkState"></param>
+        public void UpdateVehicleParkState(BlockingQueue bq, string numberPlate, int vehiclepParkState)
+        {
+            string sql = "update vehicle set vehiclepParkState = '" + vehiclepParkState + "'where numberPlate = '" + numberPlate + "'";
+            Operation oper = new Operation(bq, sql);
+            oper.getUpdate();
+        }
+        /// <summary>
+        /// 更新车位状态
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="parkingSpaceID"></param>
+        /// <param name="parkingSpaceState"></param>
+        public void UpdateParkingSpaceState(BlockingQueue bq, int parkingSpaceID, int parkingSpaceState)
+        {
+            string sql = "update parkingspace set parkingSpaceState = '" + parkingSpaceState + "'where parkingSpaceID = '" + parkingSpaceID + "'";
+            Operation oper = new Operation(bq, sql);
+            oper.getUpdate();
+        }
+    }
+}

+ 1 - 0
parkMonitor/parkMonitor.csproj

@@ -119,6 +119,7 @@
     <Compile Include="Database2\DatabaseConnPoolFactory.cs" />
     <Compile Include="Database2\DBAccess.cs" />
     <Compile Include="Database2\DBOperation.cs" />
+    <Compile Include="Database2\DBOperation2.cs" />
     <Compile Include="Database2\IDBOperation.cs" />
     <Compile Include="Database2\SimpleCommandFactory.cs" />
     <Compile Include="Database2\Vehicle.cs" />

+ 84 - 55
parkMonitor/server/CoreThread/AbstractCmd.cs

@@ -802,60 +802,82 @@ namespace parkMonitor.server.CoreThread
                     {
                         //事务,写入停车记录, 更新车辆信息
                         int parkingRecordsID = 0;
+                        string insertRecordSql = "";
+                        try
+                        {
+                            insertRecordSql = "insert into parkingrecords(userID,numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime) values('" + userID + "','" + queueCmd.LicenseNum + "','" + ppp.parkingSpaceID + "','" + garageID + "',3,'" + realParkTime + "')";
+                            List<string> strs = new List<string>();
+                            strs.Add(insertRecordSql);
+                            parkingRecordsID = DBOperation.InsertParkingRecords(EntityForCore.remoteBQ, strs);
+                        }
+                        catch (Exception ex)
+                        {
+                            Console.WriteLine(ex.Message + "插入停车记录表失败");
+                            //数据库操作失败写日志
+                            Log.WriteLog(LogType.DATABASE, "1", insertRecordSql);
+                        }
+                        string updateParkingSpaceStateSql = "";
+                        string updateFreeSpaceSql = "";
+                        string updateVehicleSql = "";
                         try
                         {
-                            string insertRecordSql = "insert into parkingrecords(userID,numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime) values('" + userID + "','" + queueCmd.LicenseNum + "','" + ppp.parkingSpaceID + "','" + garageID + "',3,'" + realParkTime + "')";
-                            if (!Operation.MyTransaction(EntityForCore.remoteBQ,insertRecordSql, out parkingRecordsID))
-                            {
-                                //数据库操作失败写日志
-                                Log.WriteLog(LogType.DATABASE, "1", insertRecordSql);
-                            }
                             int freeSpace = ParkingSpaceManager.ins.GetFreeSpaceCount();
                             List<string> strs = new List<string>();
-                            string updateParkingSpaceStateSql = "update parkingspace set parkingSpaceState = 1 where parkingSpaceID = '" + ppp.parkingSpaceID + "'";
-                            string updateFreeSpaceSql = "update garage set garageFreeSpace = '" + freeSpace + "' where garageID = '" + garageID + "'";
-                            string updateVehicleSql = "update vehicle set vehiclepParkState = 1,scanEntryTime = '" + queueCmd.TimeRecord + "',parkingRecordsID = '" + parkingRecordsID + "',parkingSpaceID = '" + ppp.parkingSpaceID + "',vehicleTypeConfirm = 1,frontwheelbase = '" + frontWheelbase + "',rearwheelbase = '" + rearWheelbase + "' where numberPlate = '" + queueCmd.LicenseNum + "'";
+                            updateParkingSpaceStateSql = "update parkingspace set parkingSpaceState = 1 where parkingSpaceID = '" + ppp.parkingSpaceID + "'";
+                            updateFreeSpaceSql = "update garage set garageFreeSpace = '" + freeSpace + "' where garageID = '" + garageID + "'";
+                            updateVehicleSql = "update vehicle set vehiclepParkState = 1,scanEntryTime = '" + queueCmd.TimeRecord + "',parkingRecordsID = '" + parkingRecordsID + "',parkingSpaceID = '" + ppp.parkingSpaceID + "',vehicleTypeConfirm = 1,frontwheelbase = '" + frontWheelbase + "',rearwheelbase = '" + rearWheelbase + "' where numberPlate = '" + queueCmd.LicenseNum + "'";
                             strs.Add(updateParkingSpaceStateSql);
                             strs.Add(updateFreeSpaceSql);
                             strs.Add(updateVehicleSql);
-                            int temp;
-                            if (!Operation.MyTransaction(EntityForCore.remoteBQ,strs, out temp))
-                            {
-                                //数据库操作失败写日志
-                                Log.WriteLog(LogType.DATABASE, "0", updateParkingSpaceStateSql);
-                                Log.WriteLog(LogType.DATABASE, "0", updateFreeSpaceSql);
-                                Log.WriteLog(LogType.DATABASE, "0", updateVehicleSql);
-                            }
+                            DBOperation.UpdateTransaction(EntityForCore.remoteBQ, strs);
                         }
-                        catch(Exception e)
+                        catch (Exception ex)
                         {
-                            UILogServer.ins.error("停车数据库操作失败");
-                            Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "\n"+e.StackTrace + "\n" + e.Message);
-                            //throw;//数据库操作失败异常待处理
+                            Console.WriteLine(ex.Message + "停车事务处理失败");
+                            //数据库操作失败写日志
+                            Log.WriteLog(LogType.DATABASE, "0", updateParkingSpaceStateSql);
+                            Log.WriteLog(LogType.DATABASE, "0", updateFreeSpaceSql);
+                            Log.WriteLog(LogType.DATABASE, "0", updateVehicleSql);
                         }
+                        catch
+                            Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "\n"+e.StackTrace + "\n" + e.Message);
                     }
                     else
                     {
                         //事务,写入停车记录, 更新车辆信息
                         int parkingRecordsID = 0;
+                        string insertRecordSql = "";
                         try
                         {
-                            string insertRecordSql = "insert into parkingrecords(userID,numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime) values('" + userID + "','" + queueCmd.LicenseNum + "','" + ppp.parkingSpaceID + "','" + garageID + "',3,'" + realParkTime + "')";
-                            if (!Operation.MyTransaction(EntityForCore.remoteBQ, insertRecordSql, out parkingRecordsID))
+                            insertRecordSql = "insert into parkingrecords(userID,numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime) values('" + userID + "','" + queueCmd.LicenseNum + "','" + ppp.parkingSpaceID + "','" + garageID + "',3,'" + realParkTime + "')";
+                            List<string> strs = new List<string>();
+                            strs.Add(insertRecordSql);
+                            parkingRecordsID = DBOperation.InsertParkingRecords(EntityForCore.localBQ, strs);
+                            if (parkingRecordsID == 0)
                             {
                                 //数据库操作失败写日志
                                 Log.WriteLog(LogType.DATABASE, "1", insertRecordSql);
                             }
+                        }
+                        catch (Exception ex)
+                        {
+                            Console.WriteLine(ex.Message + "插入停车记录表失败");                          
+                        }
+                        string updateParkingSpaceStateSql = "";
+                        string updateFreeSpaceSql = "";
+                        string updateVehicleSql = "";
+                        try
+                        {
                             int freeSpace = ParkingSpaceManager.ins.GetFreeSpaceCount();
                             List<string> strs = new List<string>();
-                            string updateParkingSpaceStateSql = "update parkingspace set parkingSpaceState = 1 where parkingSpaceID = '" + ppp.parkingSpaceID + "'";
-                            string updateFreeSpaceSql = "update garage set garageFreeSpace = '" + freeSpace + "' where garageID = '" + garageID + "'";
-                            string updateVehicleSql = "update vehicle set vehiclepParkState = 1,scanEntryTime = '" + queueCmd.TimeRecord + "',parkingRecordsID = '" + parkingRecordsID + "',parkingSpaceID = '" + ppp.parkingSpaceID + "',vehicleTypeConfirm = 1,frontwheelbase = '" + frontWheelbase + "',rearwheelbase = '" + rearWheelbase + "' where numberPlate = '" + queueCmd.LicenseNum + "'";
+                            updateParkingSpaceStateSql = "update parkingspace set parkingSpaceState = 1 where parkingSpaceID = '" + ppp.parkingSpaceID + "'";
+                            updateFreeSpaceSql = "update garage set garageFreeSpace = '" + freeSpace + "' where garageID = '" + garageID + "'";
+                            updateVehicleSql = "update vehicle set vehiclepParkState = 1,scanEntryTime = '" + queueCmd.TimeRecord + "',parkingRecordsID = '" + parkingRecordsID + "',parkingSpaceID = '" + ppp.parkingSpaceID + "',vehicleTypeConfirm = 1,frontwheelbase = '" + frontWheelbase + "',rearwheelbase = '" + rearWheelbase + "' where numberPlate = '" + queueCmd.LicenseNum + "'";
                             strs.Add(updateParkingSpaceStateSql);
                             strs.Add(updateFreeSpaceSql);
                             strs.Add(updateVehicleSql);
-                            int temp;
-                            if (!Operation.MyTransaction(EntityForCore.remoteBQ, strs, out temp))
+                            object result = DBOperation.UpdateTransaction(EntityForCore.localBQ, strs);
+                            if (!(bool)result)
                             {
                                 //数据库操作失败写日志
                                 Log.WriteLog(LogType.DATABASE, "0", updateParkingSpaceStateSql);
@@ -863,11 +885,9 @@ namespace parkMonitor.server.CoreThread
                                 Log.WriteLog(LogType.DATABASE, "0", updateVehicleSql);
                             }
                         }
-                        catch(Exception e)
+                        catch (Exception ex)
                         {
-                            UILogServer.ins.error("停车数据库操作失败");
-                            Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "\n" + e.StackTrace + "\n" + e.Message);
-                            //throw;//数据库操作失败异常待处理
+                            Console.WriteLine(ex.Message + "停车事务处理失败");                          
                         }
                     }
                 }
@@ -1084,23 +1104,25 @@ namespace parkMonitor.server.CoreThread
                 {
                     if (!queueCmd.manual)
                     {
+                        string updateParkingSpaceStateSql = "";
+                        string updateFreeSpaceSql = "";
+                        string updateVehicleStateSql = "";
+                        string updateParkingRecordsSql = "";
                         //取车事务
                         try
                         {
-
-                            int temp;
                             List<string> strs = new List<string>();
-                            string updateParkingSpaceStateSql = "update parkingspace set parkingSpaceState = 0 where parkingSpaceID = '" + ps.parkingSpaceID + "'";
-                            string updateFreeSpaceSql = "update garage set garageFreeSpace = '" + freeSpaceCount + "' where garageID = '" + garageID + "'";
-                            string updateVehicleStateSql = "update vehicle set vehiclepParkState = 0 where numberPlate = '" + queueCmd.LicenseNum + "'";
-                            string updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '" + queueCmd.TimeRecord + "'where parkingRecordsID = '" + queueCmd.parkingRecordsID + "'";
+                            updateParkingSpaceStateSql = "update parkingspace set parkingSpaceState = 1 where parkingSpaceID = '" + ps.parkingSpaceID + "'";
+                            updateFreeSpaceSql = "update garage set garageFreeSpace = '" + freeSpaceCount + "' where garageID = '" + garageID + "'";
+                            updateVehicleStateSql = "update vehicle set vehiclepParkState = 0 where numberPlate = '" + queueCmd.LicenseNum + "'";
+                            updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '" + queueCmd.TimeRecord + "'where parkingRecordsID = '" + queueCmd.parkingRecordsID + "'";
                             strs.Add(updateParkingSpaceStateSql);
                             strs.Add(updateFreeSpaceSql);
                             strs.Add(updateVehicleStateSql);
                             strs.Add(updateParkingRecordsSql);
-                            if (!Operation.MyTransaction(EntityForCore.remoteBQ,strs, out temp))
+                            object result = DBOperation.UpdateTransaction(EntityForCore.remoteBQ, strs);
+                            if (!(bool)result)
                             {
-                                Log.WriteLog(LogType.NOT_DATABASE, "数据库操作出错,记录sql语句等待流程回滚");
                                 //写日志记录sql,以待之后处理
                                 Log.WriteLog(LogType.DATABASE, "0", updateParkingSpaceStateSql);
                                 Log.WriteLog(LogType.DATABASE, "0", updateFreeSpaceSql);
@@ -1108,27 +1130,34 @@ namespace parkMonitor.server.CoreThread
                                 Log.WriteLog(LogType.DATABASE, "0", updateParkingRecordsSql);
                             }
                         }
-                        catch
+                        catch (Exception ex)
                         {
-                            UILogServer.ins.error("数据库操作失败");
-                            //throw;//数据库操作失败异常待处理
+                            Console.WriteLine(ex.Message + "取车事务处理失败");
                         }
                     }
                     else
                     {
-                        int temp;
-                        List<string> strs = new List<string>();
-                        string updateParkingSpaceStateSql = "update parkingspace set parkingSpaceState = 0 where parkingSpaceID = '" + ps.parkingSpaceID + "'";
-                        string updateFreeSpaceSql = "update garage set garageFreeSpace = '" + freeSpaceCount + "' where garageID = '" + garageID + "'";
-                        string updateVehicleStateSql = "update vehicle set vehiclepParkState = 0 where numberPlate = '" + queueCmd.LicenseNum + "'";
-                        string updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '" + queueCmd.TimeRecord + "'where parkingRecordsID = '" + queueCmd.parkingRecordsID + "'";
-                        strs.Add(updateParkingSpaceStateSql);
-                        strs.Add(updateFreeSpaceSql);
-                        strs.Add(updateVehicleStateSql);
-                        strs.Add(updateParkingRecordsSql);
-                        if (!Operation.MyTransaction(EntityForCore.localBQ, strs, out temp))
+                        string updateParkingSpaceStateSql = "";
+                        string updateFreeSpaceSql = "";
+                        string updateVehicleStateSql = "";
+                        string updateParkingRecordsSql = "";
+                        //取车事务
+                        try
+                        {
+                            List<string> strs = new List<string>();
+                            updateParkingSpaceStateSql = "update parkingspace set parkingSpaceState = 0 where parkingSpaceID = '" + ps.parkingSpaceID + "'";
+                            updateFreeSpaceSql = "update garage set garageFreeSpace = '" + freeSpaceCount + "' where garageID = '" + garageID + "'";
+                            updateVehicleStateSql = "update vehicle set vehiclepParkState = 0 where numberPlate = '" + queueCmd.LicenseNum + "'";
+                            updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '" + queueCmd.TimeRecord + "'where parkingRecordsID = '" + queueCmd.parkingRecordsID + "'";
+                            strs.Add(updateParkingSpaceStateSql);
+                            strs.Add(updateFreeSpaceSql);
+                            strs.Add(updateVehicleStateSql);
+                            strs.Add(updateParkingRecordsSql);
+                            DBOperation.UpdateTransaction(EntityForCore.localBQ, strs);
+                        }
+                        catch (Exception ex)
                         {
-                            Log.WriteLog(LogType.NOT_DATABASE, "数据库操作出错,记录sql语句等待流程回滚");
+                            Console.WriteLine(ex.Message + "取车事务处理失败");
                             //写日志记录sql,以待之后处理
                             Log.WriteLog(LogType.DATABASE, "0", updateParkingSpaceStateSql);
                             Log.WriteLog(LogType.DATABASE, "0", updateFreeSpaceSql);

+ 8 - 4
parkMonitor/server/CoreThread/CoreThreadTest2.cs

@@ -109,8 +109,8 @@ namespace parkMonitor.server.CoreThread
         /// </summary>
         public void BeginWorking()
         {
-            Timer checkRemoteTimer = new System.Threading.Timer(CheckRemotePool, null, 300000, 300000);
-            Timer checkLocalTimer = new System.Threading.Timer(CheckLocalPool, null, 300000, 300000);
+            Timer checkRemoteTimer = new System.Threading.Timer(CheckRemotePool, null, 100000, 300000);
+            Timer checkLocalTimer = new System.Threading.Timer(CheckLocalPool, null, 100000, 300000);
             //Timer updateTimer = new System.Threading.Timer(updateGarageAndParkingSpace, null, 1000, 3000);
             Timer logTimer = new System.Threading.Timer(displayLog, null, 1000, 1800000);
             Object lockObj = new object();
@@ -242,6 +242,8 @@ namespace parkMonitor.server.CoreThread
                 string sqlMsg;
                 int count;
                 Log.ReadLog(out sqlStatus, out sqlMsg, out count);
+                List<string> strs = new List<string>();
+                strs.Add(sqlMsg);
                 if (count > 0)
                 {
                     if (sqlStatus == "1")
@@ -249,7 +251,8 @@ namespace parkMonitor.server.CoreThread
                         int parkingRecordsID = 0;
                         try
                         {
-                            Operation.MyTransaction(EntityForCore.remoteBQ,sqlMsg, out parkingRecordsID);
+                            //Operation.MyTransaction(EntityForCore.remoteBQ,sqlMsg, out parkingRecordsID);
+                            parkingRecordsID = DBOperation.InsertParkingRecords(EntityForCore.remoteBQ, strs);
                         }
                         catch
                         {
@@ -262,7 +265,8 @@ namespace parkMonitor.server.CoreThread
                         int temp;
                         try
                         {
-                            Operation.MyTransaction(EntityForCore.remoteBQ,sqlMsg, out temp); 
+                            //Operation.MyTransaction(EntityForCore.remoteBQ,sqlMsg, out temp); 
+                            DBOperation.InsertParkingRecords(EntityForCore.remoteBQ, strs);
                         }
                         catch
                         {