Преглед изворни кода

基本完成XML解析功能

章政 пре 8 година
родитељ
комит
8cda07923d
3 измењених фајлова са 383 додато и 123 уклоњено
  1. 266 63
      UAS_XmlAnalysor/DataHelper.cs
  2. 65 53
      UAS_XmlAnalysor/Form1.Designer.cs
  3. 52 7
      UAS_XmlAnalysor/Form1.cs

+ 266 - 63
UAS_XmlAnalysor/DataHelper.cs

@@ -26,18 +26,12 @@ namespace UAS_XmlAnalysor
             {
                 //如果选择的是默认数据则直接用配置文件的信息连接,否则选择数据库的账套信息
                 if (DBConnectionString == null || DBConnectionString == ConnectionStrings)
-                {
                     connection = new OracleConnection(ConnectionStrings);
-                }
                 else
-                {
                     connection = new OracleConnection(DBConnectionString);
-                }
                 connection.Open();
-                command = new OracleCommand();
-                command.Connection = connection;
             }
-            catch (Exception ) {  }
+            catch (Exception e) { }
         }
 
         /// <summary>
@@ -46,12 +40,12 @@ namespace UAS_XmlAnalysor
         public DataTable GetColumnDataType(string TableName)
         {
             DataTable dt = new DataTable();
-            command.CommandText = "select Column_Name,Data_Type from cols where TABLE_name=upper('" + TableName + "')";
-            command.CommandType = CommandType.Text;
+            command = new OracleCommand("select Column_Name,Data_Type from cols where TABLE_name=upper('" + TableName + "')", connection);
             Reconnect(command);
             OracleDataAdapter ad = new OracleDataAdapter(command);
             ad.Fill(dt);
             ad.Dispose();
+            command.Dispose();
             return dt;
         }
 
@@ -62,13 +56,14 @@ namespace UAS_XmlAnalysor
         {
             DataTable dt = new DataTable();
             string sql = "select " + Field + " from " + TableName + " where " + Condition;
-            command.CommandText = sql;
-            command.CommandType = CommandType.Text;
+
+            command = new OracleCommand(sql, connection);
             Reconnect(command);
             OracleDataAdapter ad = new OracleDataAdapter();
             ad.SelectCommand = command;
             ad.Fill(dt);
             ad.Dispose();
+            command.Dispose();
             if (dt.Rows.Count > 0)
             {
                 return dt.Rows[0][0];
@@ -79,6 +74,7 @@ namespace UAS_XmlAnalysor
             }
         }
 
+
         /// <summary>
         /// 执行打印的SQL
         /// </summary>
@@ -98,17 +94,18 @@ namespace UAS_XmlAnalysor
                 {
                     sb.Append(Param[i] + ":Param" + i);
                 }
-                command.CommandText = sb.ToString();
-                command.CommandType = CommandType.Text;
+                command = new OracleCommand(sb.ToString(), connection);
+
                 for (int i = 0; i < ParamNum; i++)
                 {
                     command.Parameters.Add("Param" + i, OracleDbType.Varchar2, Parameters[i], ParameterDirection.Input);
                 }
+
                 OracleDataAdapter ad = new OracleDataAdapter(command);
                 DataTable dt = new DataTable();
                 ad.Fill(dt);
                 ad.Dispose();
-                command.Parameters.Clear();
+                command.Dispose();
                 return dt;
             }
             return "参数错误,请检查SQL语句";
@@ -122,12 +119,13 @@ namespace UAS_XmlAnalysor
         {
             DataTable dt = new DataTable();
             string sql = "select count(1) from " + TableName + " where " + Condition;
-            command.CommandText = sql;
-            command.CommandType = CommandType.Text;
+
+            command = new OracleCommand(sql, connection);
             Reconnect(command);
             OracleDataAdapter ad = new OracleDataAdapter(command);
             ad.Fill(dt);
             ad.Dispose();
+            command.Dispose();
             return int.Parse(dt.Rows[0][0].ToString());
         }
 
@@ -140,12 +138,13 @@ namespace UAS_XmlAnalysor
         {
             DataTable dt = new DataTable();
             string sql = "select count(1) from " + TableName;
-            command.CommandText = sql;
-            command.CommandType = CommandType.Text;
+            command = new OracleCommand(sql, connection);
             Reconnect(command);
             OracleDataAdapter ad = new OracleDataAdapter(command);
+
             ad.Fill(dt);
             ad.Dispose();
+            command.Dispose();
             return int.Parse(dt.Rows[0][0].ToString());
         }
 
@@ -158,15 +157,32 @@ namespace UAS_XmlAnalysor
             string sql = "select ";
             sql += AddField(Fields);
             sql += " from " + TableName + " where " + Condition + " and rownum=1";
-            command.CommandText = sql;
-            command.CommandType = CommandType.Text;
+
+            command = new OracleCommand(sql, connection);
             Reconnect(command);
             OracleDataAdapter ad = new OracleDataAdapter(command);
             ad.Fill(dt);
             ad.Dispose();
+            command.Dispose();
             return dt;
         }
 
+        /// <summary>
+        /// 将DataTable导入到指定的表中
+        /// </summary>
+        /// <param name="DataTable"></param>
+        /// <param name="TableName"></param>
+        public void InsertDataTable(DataTable DataTable, string TableName)
+        {
+            for (int i = 0; i < DataTable.Rows.Count; i++)
+            {
+                for (int j = 0; j < DataTable.Columns.Count; j++)
+                {
+
+                }
+            }
+        }
+
         /// <summary>
         /// 按分页获取数据
         /// </summary>
@@ -195,12 +211,12 @@ namespace UAS_XmlAnalysor
                 else
                     sql.Append(" from " + TableName + ") A where ROWNUM <= " + CurrentPage * PageSize + ") where RN> " + (CurrentPage - 1) * PageSize);
             }
-            command.CommandText = sql.ToString();
-            command.CommandType = CommandType.Text;
+            command = new OracleCommand(sql.ToString(), connection);
             Reconnect(command);
             OracleDataAdapter ad = new OracleDataAdapter(command);
             ad.Fill(dt);
             ad.Dispose();
+            command.Dispose();
             dt.Columns.RemoveAt(0);
             foreach (DataColumn dc in dt.Columns)
             {
@@ -219,12 +235,13 @@ namespace UAS_XmlAnalysor
             string sql = "select ";
             sql += AddField(Fields);
             sql += " from " + TableName + " where " + Condition;
-            command.CommandText = sql;
-            command.CommandType = CommandType.Text;
+
+            command = new OracleCommand(sql, connection);
             Reconnect(command);
             OracleDataAdapter ad = new OracleDataAdapter(command);
             ad.Fill(dt);
             ad.Dispose();
+            command.Dispose();
             return dt;
         }
 
@@ -237,8 +254,7 @@ namespace UAS_XmlAnalysor
             string sql = "select ";
             sql += Fields;
             sql += " from " + TableName;
-            command.CommandText = sql;
-            command.CommandType = CommandType.Text;
+            command = new OracleCommand(sql, connection);
             Reconnect(command);
             OracleDataAdapter ad = new OracleDataAdapter(command);
             ad.SelectCommand = command;
@@ -248,6 +264,7 @@ namespace UAS_XmlAnalysor
                 dc.Caption = "测试测试";
             }
             ad.Dispose();
+            command.Dispose();
             return dt;
         }
 
@@ -326,6 +343,7 @@ namespace UAS_XmlAnalysor
             //不是新增行的启用更新的方法
             sb.Append("update " + TableName + " set ");
             //拼接语句,特殊处理日期
+
             foreach (DataColumn dc in DataTable.Columns)
             {
                 if (!dc.DataType.ToString().Equals("System.DateTime"))
@@ -339,8 +357,7 @@ namespace UAS_XmlAnalysor
             }
             sb.Remove(sb.Length - 1, 1);
             sb.Append(" where " + PrimaryKey + "=:" + PrimaryKey);
-            command.CommandText = sb.ToString();
-            command.CommandType = CommandType.Text;
+            command = new OracleCommand(sb.ToString(), connection);
             OracleDataAdapter ad = new OracleDataAdapter(command);
             // 参数的长度是DataTable的行数决定的
             command.ArrayBindCount = DataTable.Rows.Count;
@@ -369,7 +386,169 @@ namespace UAS_XmlAnalysor
             ad.UpdateCommand = command;
             ad.Update(DataTable);
             ad.Dispose();
-            command.Parameters.Clear();
+            command.Dispose();
+        }
+
+        /// <summary>
+        /// 获取DbFind的数据的DataTable的结构
+        /// </summary>
+        /// <param name="field"></param>
+        /// <param name="caller"></param>
+        /// <returns></returns>
+        public DataTable GetDbFindDataTable(string field, string caller)
+        {
+            string sql = "select * from dbfindsetui where ds_caller='" + caller + "' and ds_whichui='" + field + "'";
+            DataTable dt = (DataTable)ExecuteSql(sql, "select");
+            if (dt.Rows.Count != 0)
+            {
+                //通过#号分割字段
+                string[] dbfield = dt.Rows[0]["ds_findtoui"].ToString().Split('#');
+                string[] cnfield = dt.Rows[0]["ds_dbcaption"].ToString().Split('#');
+                //获取查询要查询的Table
+                string dbtable = dt.Rows[0]["ds_tables"].ToString();
+                //拼接查询的字段
+                for (int i = 0; i < dbfield.Length; i++)
+                {
+                    dbfield[i] = dbfield[i].Split(',')[0];
+                }
+                //新建一个空的DataTable
+                DataTable dt1 = new DataTable();
+                //往空的DataTable添加结构,ColumnName是中文,Caption是实际的字段名称
+                for (int i = 0; i < cnfield.Length; i++)
+                {
+                    dt1.Columns.Add(cnfield[i]);
+                    dt1.Columns[i].Caption = dbfield[i];
+                }
+                //返回一个带有结构的空的DataTable
+                //DbFind.BindTable1 = dbtable;
+                return dt1;
+            }
+            else
+            {
+                return null;
+            }
+        }
+
+        /// <summary>
+        ///  获取配置列表中的数据,支持DaatList,Form,DetailGrid
+        /// </summary>
+        /// <param name="Caller"></param>
+        /// <param name="Type"></param>
+        /// <param name="condition"></param>
+        /// <returns></returns>
+        public DataTable GetConfigureData(string Caller, string Type, string condition)
+        {
+            DataTable dt = new DataTable();
+            //用于拼接SQL语句
+            StringBuilder Sql = new StringBuilder();
+            //用于设置不同Type时设置对应表的字段
+            string getField = "";
+            string getCaption = "";
+            string getTable = "";
+            switch (Type.ToUpper())
+            {
+                case "DATALIST":
+                    getField = "dld_field"; getCaption = "dld_caption"; getTable = "dld_table";
+                    Sql.Append("select * from datalistdetail where dld_caller='" + Caller + "'");
+                    break;
+                case "FORM":
+                    getField = "fd_field"; getCaption = "fd_caption"; getTable = "fd_table";
+                    Sql.Append("select * from formdetail where fd_foid=( select fo_id from form where fo_caller='" + Caller + "')");
+                    break;
+                case "DETAILGRID":
+                    getField = "dg_field"; getCaption = "dg_caption"; getTable = "dg_table";
+                    Sql.Append("select * from detailgrid  where dg_caller='" + Caller + "'");
+                    break;
+            }
+            command = new OracleCommand(Sql.ToString(), connection);
+            OracleDataAdapter ad = new OracleDataAdapter(command);
+            ad.Fill(dt);
+            //清除掉之前的内容重新拼接
+            Sql.Clear();
+            Sql.Append("select ");
+            string[] field = new string[dt.Rows.Count];
+            string[] caption = new string[dt.Rows.Count];
+            DataTable dt1 = new DataTable();
+            //记录描述和字段名称
+            foreach (DataRow dr in dt.Rows)
+            {
+                field[dt.Rows.IndexOf(dr)] = dr[getCaption].ToString();
+                caption[dt.Rows.IndexOf(dr)] = dr[getField].ToString();
+                Sql.Append(dr[getField] + ",");
+            }
+            //调用substring是为了去除之前拼接多出来的一个逗号
+            string sql = Sql.Remove(Sql.Length - 1, 1).ToString() + " from " + dt.Rows[0][getTable] + " where " + condition;
+            //调用一个新的构造DataTable用来存放返回的数据
+            dt1 = (DataTable)ExecuteSql(sql, "select");
+            //给DataTable加上列名和描述,列名是中文字段,描述是数据库实际的字段名称
+            for (int i = 0; i < field.Length; i++)
+            {
+                dt1.Columns[i].ColumnName = field[i];
+                dt1.Columns[i].Caption = caption[i];
+            }
+            //返回的第一条数据是SQL,后面的是实际的列名
+            ad.Dispose();
+            command.Dispose();
+            return dt1;
+        }
+
+        /// <summary>
+        /// 查询配置的字段,Type是查询DataList,Form还是DetailGrid
+        /// </summary>
+        /// <param name="Caller"></param>
+        /// <param name="Type"></param>
+        /// <returns></returns>
+        public DataTable GetConfigureData(string Caller, string Type)
+        {
+            DataTable dt = new DataTable();
+            //用于拼接SQL语句
+            StringBuilder Sql = new StringBuilder();
+            //用于设置不同Type时设置对应表的字段
+            string getField = "";
+            string getCaption = "";
+            string getTable = "";
+            switch (Type.ToUpper())
+            {
+                case "DATALIST":
+                    getField = "dld_field"; getCaption = "dld_caption"; getTable = "dld_table";
+                    Sql.Append("select * from datalistdetail where dld_caller='" + Caller + "'");
+                    break;
+                case "FORM":
+                    getField = "fd_field"; getCaption = "fd_caption"; getTable = "fd_table";
+                    Sql.Append("select * from formdetail where fd_foid=( select fo_id from form where fo_caller='" + Caller + "')");
+                    break;
+                case "DETAILGRID":
+                    getField = "dg_field"; getCaption = "dg_caption"; getTable = "dg_table";
+                    Sql.Append("select * from detailgrid  where dg_caller='" + Caller + "'");
+                    break;
+            }
+            command = new OracleCommand(Sql.ToString(), connection);
+            OracleDataAdapter ad = new OracleDataAdapter(command);
+            ad.Fill(dt);
+            //清除掉之前的内容重新拼接
+            Sql.Clear();
+            Sql.Append("select ");
+            //用于记录实际的列名,+1的目的是为了存放SQL
+            string[] field = new string[dt.Rows.Count];
+            string[] caption = new string[dt.Rows.Count];
+            DataTable dt1 = new DataTable();
+            foreach (DataRow dr in dt.Rows)
+            {
+                field[dt.Rows.IndexOf(dr)] = dr[getCaption].ToString();
+                caption[dt.Rows.IndexOf(dr)] = dr[getField].ToString();
+                Sql.Append(dr[getField] + ",");
+            }
+            string sql = Sql.Remove(Sql.Length - 1, 1).ToString() + " from " + dt.Rows[0][getTable];
+            dt1 = (DataTable)ExecuteSql(sql, "select");
+            //设置DataTable的列名和描述
+            for (int i = 0; i < field.Length; i++)
+            {
+                dt1.Columns[i].ColumnName = field[i];
+                dt1.Columns[i].Caption = caption[i];
+            }
+            ad.Dispose();
+            command.Dispose();
+            return dt1;
         }
 
         /// <summary>
@@ -381,14 +560,14 @@ namespace UAS_XmlAnalysor
         public bool CheckExist(string TableName, string Condition)
         {
             string sql = "select count(1) from " + TableName + " where " + Condition;
-            command.CommandText = sql;
-            command.CommandType = CommandType.Text;
+            command = new OracleCommand(sql, connection);
             Reconnect(command);
             OracleDataAdapter ad = new OracleDataAdapter(command);
             DataTable dt = new DataTable();
 
             ad.Fill(dt);
             ad.Dispose();
+            command.Dispose();
             return int.Parse(dt.Rows[0][0].ToString()) > 0;
         }
 
@@ -401,8 +580,7 @@ namespace UAS_XmlAnalysor
         public object ExecuteSql(string SQL, string Type, params object[] names)
         {
             object result = null;
-            command.CommandText = SQL;
-            command.CommandType = CommandType.Text;
+            command = new OracleCommand(SQL, connection);
             Reconnect(command);
             //用来拼接参数的
             if (names.Length > 0)
@@ -435,9 +613,24 @@ namespace UAS_XmlAnalysor
             {
                 case "SELECT":
                     result = new DataTable();
-                    OracleDataAdapter ad = new OracleDataAdapter(command);
-                    ad.Fill((DataTable)result);
-                    ad.Dispose();
+                    try
+                    {
+                        OracleDataAdapter ad = new OracleDataAdapter(command);
+                        ad.Fill((DataTable)result);
+                        ad.Dispose();
+                        //成功执行后将重复连接数置为0
+                        ReconnectTime = 0;
+                    }
+                    catch (Exception)
+                    {
+                        if (ReconnectTime == 0)
+                        {
+                            //重置的数据库链接后只执行一次
+                            ReconnectTime = ReconnectTime + 1;
+                            connection = new OracleConnection(DBConnectionString);
+                            result = ExecuteSql(SQL, Type, names);
+                        }
+                    }
                     break;
                 case "DELETE":
                     result = command.ExecuteNonQuery();
@@ -449,21 +642,33 @@ namespace UAS_XmlAnalysor
                     result = command.ExecuteNonQuery();
                     break;
             }
-            command.Parameters.Clear();
+            command.Dispose();
             return result;
         }
 
+        /// <summary>
+        /// 为了同步BS端的条码维护,检测时允许问号的存在,在检测时默认将问号换成:Param参数
+        /// </summary>
+        /// <param name="SQL"></param>
+        public void CheckSQL(string SQL)
+        {
+            SQL = SQL.Replace("?", ":Param");
+            command = new OracleCommand(SQL, connection);
+            command.ExecuteNonQuery();
+            command.Dispose();
+        }
+
         public int GetDistinctRowCount(string TableName, string Field)
         {
             DataTable dt = new DataTable();
             string sql = "select distinct count('" + Field + "') from " + TableName;
 
-            command.CommandText = sql;
-            command.CommandType = CommandType.Text;
+            command = new OracleCommand(sql, connection);
             Reconnect(command);
             OracleDataAdapter ad = new OracleDataAdapter(command);
             ad.Fill(dt);
             ad.Dispose();
+            command.Dispose();
             return int.Parse(dt.Rows[0][0].ToString());
         }
 
@@ -489,13 +694,12 @@ namespace UAS_XmlAnalysor
         {
             string sql = "delete from " + TableName + " where " + ID + " =:DeleteID";
 
-            command.CommandText = sql;
-            command.CommandType = CommandType.Text;
+            command = new OracleCommand(sql, connection);
             Reconnect(command);
             command.ArrayBindCount = DeleteID.Length;
             command.Parameters.Add(new OracleParameter("DeleteID", OracleDbType.Long, DeleteID, ParameterDirection.Input));
             command.ExecuteNonQuery();
-            command.Parameters.Clear();
+            command.Dispose();
         }
 
         /// <summary>
@@ -633,11 +837,9 @@ namespace UAS_XmlAnalysor
         /// <param name="names"></param>
         public void BatchInsert(string sql, params object[][] names)
         {
-            command.CommandText = sql;
-            command.CommandType = CommandType.Text;
+            command = new OracleCommand(sql, connection);
             Reconnect(command);
             command.ArrayBindCount = names[1].Length;
-
             //因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
             //将第一个数组的下标固定为0作为循环添加的参数的名称
             for (int i = 1; i <= names[0].Length; i++)
@@ -645,13 +847,12 @@ namespace UAS_XmlAnalysor
                 command.Parameters.Add(new OracleParameter(names[0][i - 1].ToString(), OracleDbType.Varchar2, names[i], ParameterDirection.Input));
             }
             command.ExecuteNonQuery();
-            command.Parameters.Clear();
+            command.Dispose();
         }
 
         public void BatchInsertDataTable(string sql, string[] param, params object[][] param1)
         {
-            command.CommandText = sql;
-            command.CommandType = CommandType.Text;
+            command = new OracleCommand(sql, connection);
             Reconnect(command);
             command.ArrayBindCount = param1[0].Length;
             //因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
@@ -661,7 +862,7 @@ namespace UAS_XmlAnalysor
                 command.Parameters.Add(new OracleParameter(param[i].ToString(), OracleDbType.Varchar2, param1[i], ParameterDirection.Input));
             }
             command.ExecuteNonQuery();
-            command.Parameters.Clear();
+            command.Dispose();
         }
 
 
@@ -675,12 +876,12 @@ namespace UAS_XmlAnalysor
         {
             DataTable dt = new DataTable();
             string SQL = " select listagg(dld_field,',') within group (order by dld_id)  from datalistdetail where dld_caller='" + Caller + "'";
-            command.CommandText = SQL;
-            command.CommandType = CommandType.Text;
+            command = new OracleCommand(SQL, connection);
             Reconnect(command);
             OracleDataAdapter ad = new OracleDataAdapter(command);
             ad.Fill(dt);
             ad.Dispose();
+            command.Dispose();
             return dt.Rows[0][0].ToString();
         }
 
@@ -750,10 +951,10 @@ namespace UAS_XmlAnalysor
         {
             string sql = "update " + TableName + " set " + update + " where " + condition;
 
-            command.CommandText = sql;
-            command.CommandType = CommandType.Text;
+            command = new OracleCommand(sql, connection);
             Reconnect(command);
             command.ExecuteNonQuery();
+            command.Dispose();
             return sql;
         }
 
@@ -764,15 +965,17 @@ namespace UAS_XmlAnalysor
         /// <param name="param"></param>
         public void CallProcedure(string ProcedureName, ref string[] param)
         {
-            command.CommandText = ProcedureName;
+            command = new OracleCommand(ProcedureName);
+            command.Connection = connection;
             Reconnect(command);
+            command.CommandText = ProcedureName;
             command.CommandType = CommandType.StoredProcedure;
             for (int i = 0; i < param.Length; i++)
                 command.Parameters.Add(new OracleParameter(param[i].ToString(), OracleDbType.Varchar2, 200, param[i], ParameterDirection.InputOutput));
             command.ExecuteNonQuery();
             for (int i = 0; i < command.Parameters.Count; i++)
                 param[i] = command.Parameters[i].Value.ToString();
-            command.Parameters.Clear();
+            command.Dispose();
         }
 
         /// <summary>
@@ -782,8 +985,9 @@ namespace UAS_XmlAnalysor
         public void ExecuteSQLTran(params string[] SQL)
         {
             OracleTransaction tx = connection.BeginTransaction();
+            command = new OracleCommand();
+            command.Connection = connection;
             command.Transaction = tx;
-            command.CommandType = CommandType.Text;
             try
             {
                 foreach (string sql in SQL)
@@ -801,6 +1005,7 @@ namespace UAS_XmlAnalysor
             {
                 tx.Rollback();
             }
+            command.Dispose();
         }
 
         /// <summary>
@@ -850,8 +1055,7 @@ namespace UAS_XmlAnalysor
         public object GetLabelParam(string sql)
         {
             DataTable dt = new DataTable();
-            command.CommandText = sql;
-            command.CommandType = CommandType.Text;
+            command = new OracleCommand(sql, connection);
             Reconnect(command);
             OracleDataAdapter ad = new OracleDataAdapter();
             ad.SelectCommand = command;
@@ -859,10 +1063,12 @@ namespace UAS_XmlAnalysor
             if (dt.Rows.Count > 0)
             {
                 ad.Dispose();
+                command.Dispose();
                 return dt.Rows[0][0];
             }
             else
             {
+                command.Dispose();
                 return "";
             }
         }
@@ -878,6 +1084,7 @@ namespace UAS_XmlAnalysor
                 //新建一个char类型的数组用来存储每个字节的变量
                 char[] c = par[i + 1].ToCharArray();
                 addpar[i] = new StringBuilder();
+
                 for (int j = 0; j < c.Length; j++)
                 {
                     if (c[j] != ' ' && c[j] != ',' && c[j] != ')')
@@ -897,13 +1104,9 @@ namespace UAS_XmlAnalysor
             return param;
         }
 
-        /// <summary>
-        /// 释放资源
-        /// </summary>
         public void Dispose()
         {
-            command.Dispose();
-            connection.Dispose();
+
         }
 
         private void Reconnect(OracleCommand cmd)

+ 65 - 53
UAS_XmlAnalysor/Form1.Designer.cs

@@ -34,14 +34,13 @@
             this.StopWatch = new System.Windows.Forms.Button();
             this.label1 = new System.Windows.Forms.Label();
             this.FolderPath = new System.Windows.Forms.TextBox();
-            this.TimeSpan = new System.Windows.Forms.NumericUpDown();
-            this.label2 = new System.Windows.Forms.Label();
             this.ChooseFolder = new System.Windows.Forms.Button();
             this.OperateResult = new System.Windows.Forms.RichTextBox();
             this.Clean = new System.Windows.Forms.Button();
-            this.label3 = new System.Windows.Forms.Label();
+            this.ChooseBackUpFolder = new System.Windows.Forms.Button();
+            this.BackUpFolderPath = new System.Windows.Forms.TextBox();
+            this.label2 = new System.Windows.Forms.Label();
             ((System.ComponentModel.ISupportInitialize)(this.XmlWatcher)).BeginInit();
-            ((System.ComponentModel.ISupportInitialize)(this.TimeSpan)).BeginInit();
             this.SuspendLayout();
             // 
             // XmlWatcher
@@ -53,9 +52,10 @@
             // StartWatch
             // 
             this.StartWatch.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.StartWatch.Location = new System.Drawing.Point(193, 249);
+            this.StartWatch.Location = new System.Drawing.Point(145, 199);
+            this.StartWatch.Margin = new System.Windows.Forms.Padding(2);
             this.StartWatch.Name = "StartWatch";
-            this.StartWatch.Size = new System.Drawing.Size(85, 35);
+            this.StartWatch.Size = new System.Drawing.Size(64, 28);
             this.StartWatch.TabIndex = 0;
             this.StartWatch.Text = "开始监控";
             this.StartWatch.UseVisualStyleBackColor = true;
@@ -64,9 +64,10 @@
             // StopWatch
             // 
             this.StopWatch.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.StopWatch.Location = new System.Drawing.Point(332, 249);
+            this.StopWatch.Location = new System.Drawing.Point(249, 199);
+            this.StopWatch.Margin = new System.Windows.Forms.Padding(2);
             this.StopWatch.Name = "StopWatch";
-            this.StopWatch.Size = new System.Drawing.Size(85, 35);
+            this.StopWatch.Size = new System.Drawing.Size(64, 28);
             this.StopWatch.TabIndex = 1;
             this.StopWatch.Text = "停止监控";
             this.StopWatch.UseVisualStyleBackColor = true;
@@ -76,42 +77,29 @@
             // 
             this.label1.AutoSize = true;
             this.label1.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.label1.Location = new System.Drawing.Point(36, 94);
+            this.label1.Location = new System.Drawing.Point(27, 82);
+            this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
             this.label1.Name = "label1";
-            this.label1.Size = new System.Drawing.Size(107, 25);
+            this.label1.Size = new System.Drawing.Size(84, 20);
             this.label1.TabIndex = 2;
-            this.label1.Text = "检测文件夹";
+            this.label1.Text = "监控文件夹";
             // 
             // FolderPath
             // 
-            this.FolderPath.Location = new System.Drawing.Point(149, 94);
+            this.FolderPath.Enabled = false;
+            this.FolderPath.Location = new System.Drawing.Point(112, 82);
+            this.FolderPath.Margin = new System.Windows.Forms.Padding(2);
             this.FolderPath.Name = "FolderPath";
-            this.FolderPath.Size = new System.Drawing.Size(292, 25);
+            this.FolderPath.Size = new System.Drawing.Size(220, 21);
             this.FolderPath.TabIndex = 3;
             // 
-            // TimeSpan
-            // 
-            this.TimeSpan.Location = new System.Drawing.Point(149, 168);
-            this.TimeSpan.Name = "TimeSpan";
-            this.TimeSpan.Size = new System.Drawing.Size(292, 25);
-            this.TimeSpan.TabIndex = 4;
-            // 
-            // label2
-            // 
-            this.label2.AutoSize = true;
-            this.label2.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.label2.Location = new System.Drawing.Point(36, 168);
-            this.label2.Name = "label2";
-            this.label2.Size = new System.Drawing.Size(88, 25);
-            this.label2.TabIndex = 5;
-            this.label2.Text = "时间间隔";
-            // 
             // ChooseFolder
             // 
             this.ChooseFolder.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.ChooseFolder.Location = new System.Drawing.Point(462, 94);
+            this.ChooseFolder.Location = new System.Drawing.Point(346, 82);
+            this.ChooseFolder.Margin = new System.Windows.Forms.Padding(2);
             this.ChooseFolder.Name = "ChooseFolder";
-            this.ChooseFolder.Size = new System.Drawing.Size(104, 30);
+            this.ChooseFolder.Size = new System.Drawing.Size(78, 24);
             this.ChooseFolder.TabIndex = 6;
             this.ChooseFolder.Text = "选择文件夹";
             this.ChooseFolder.UseVisualStyleBackColor = true;
@@ -119,53 +107,77 @@
             // 
             // OperateResult
             // 
-            this.OperateResult.Location = new System.Drawing.Point(628, 12);
+            this.OperateResult.Location = new System.Drawing.Point(446, 10);
+            this.OperateResult.Margin = new System.Windows.Forms.Padding(2);
             this.OperateResult.Name = "OperateResult";
-            this.OperateResult.Size = new System.Drawing.Size(228, 267);
+            this.OperateResult.Size = new System.Drawing.Size(197, 214);
             this.OperateResult.TabIndex = 7;
             this.OperateResult.Text = "";
             // 
             // Clean
             // 
             this.Clean.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.Clean.Location = new System.Drawing.Point(705, 288);
+            this.Clean.Location = new System.Drawing.Point(516, 230);
+            this.Clean.Margin = new System.Windows.Forms.Padding(2);
             this.Clean.Name = "Clean";
-            this.Clean.Size = new System.Drawing.Size(74, 30);
+            this.Clean.Size = new System.Drawing.Size(56, 24);
             this.Clean.TabIndex = 8;
             this.Clean.Text = "清除";
             this.Clean.UseVisualStyleBackColor = true;
             this.Clean.Click += new System.EventHandler(this.Clean_Click);
             // 
-            // label3
+            // ChooseBackUpFolder
+            // 
+            this.ChooseBackUpFolder.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.ChooseBackUpFolder.Location = new System.Drawing.Point(346, 144);
+            this.ChooseBackUpFolder.Margin = new System.Windows.Forms.Padding(2);
+            this.ChooseBackUpFolder.Name = "ChooseBackUpFolder";
+            this.ChooseBackUpFolder.Size = new System.Drawing.Size(78, 24);
+            this.ChooseBackUpFolder.TabIndex = 11;
+            this.ChooseBackUpFolder.Text = "选择文件夹";
+            this.ChooseBackUpFolder.UseVisualStyleBackColor = true;
+            this.ChooseBackUpFolder.Click += new System.EventHandler(this.ChooseBackUpFolder_Click);
             // 
-            this.label3.AutoSize = true;
-            this.label3.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.label3.Location = new System.Drawing.Point(457, 168);
-            this.label3.Name = "label3";
-            this.label3.Size = new System.Drawing.Size(50, 25);
-            this.label3.TabIndex = 9;
-            this.label3.Text = "分钟";
+            // BackUpFolderPath
+            // 
+            this.BackUpFolderPath.Enabled = false;
+            this.BackUpFolderPath.Location = new System.Drawing.Point(112, 144);
+            this.BackUpFolderPath.Margin = new System.Windows.Forms.Padding(2);
+            this.BackUpFolderPath.Name = "BackUpFolderPath";
+            this.BackUpFolderPath.Size = new System.Drawing.Size(220, 21);
+            this.BackUpFolderPath.TabIndex = 10;
+            // 
+            // label2
+            // 
+            this.label2.AutoSize = true;
+            this.label2.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.label2.Location = new System.Drawing.Point(27, 144);
+            this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
+            this.label2.Name = "label2";
+            this.label2.Size = new System.Drawing.Size(84, 20);
+            this.label2.TabIndex = 9;
+            this.label2.Text = "备份文件夹";
             // 
             // Form1
             // 
-            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
+            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.ClientSize = new System.Drawing.Size(887, 324);
-            this.Controls.Add(this.label3);
+            this.ClientSize = new System.Drawing.Size(665, 259);
+            this.Controls.Add(this.ChooseBackUpFolder);
+            this.Controls.Add(this.BackUpFolderPath);
+            this.Controls.Add(this.label2);
             this.Controls.Add(this.Clean);
             this.Controls.Add(this.OperateResult);
             this.Controls.Add(this.ChooseFolder);
-            this.Controls.Add(this.label2);
-            this.Controls.Add(this.TimeSpan);
             this.Controls.Add(this.FolderPath);
             this.Controls.Add(this.label1);
             this.Controls.Add(this.StopWatch);
             this.Controls.Add(this.StartWatch);
+            this.Margin = new System.Windows.Forms.Padding(2);
             this.Name = "Form1";
             this.Text = "测试数据监测";
             this.Load += new System.EventHandler(this.Form1_Load);
             ((System.ComponentModel.ISupportInitialize)(this.XmlWatcher)).EndInit();
-            ((System.ComponentModel.ISupportInitialize)(this.TimeSpan)).EndInit();
             this.ResumeLayout(false);
             this.PerformLayout();
 
@@ -179,12 +191,12 @@
         private System.Windows.Forms.Button StartWatch;
         private System.Windows.Forms.Label label1;
         private System.Windows.Forms.TextBox FolderPath;
-        private System.Windows.Forms.NumericUpDown TimeSpan;
-        private System.Windows.Forms.Label label2;
         private System.Windows.Forms.Button ChooseFolder;
         private System.Windows.Forms.RichTextBox OperateResult;
         private System.Windows.Forms.Button Clean;
-        private System.Windows.Forms.Label label3;
+        private System.Windows.Forms.Button ChooseBackUpFolder;
+        private System.Windows.Forms.TextBox BackUpFolderPath;
+        private System.Windows.Forms.Label label2;
     }
 }
 

+ 52 - 7
UAS_XmlAnalysor/Form1.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Collections.Generic;
 using System.IO;
 using System.Windows.Forms;
 using System.Xml;
@@ -7,17 +8,30 @@ namespace UAS_XmlAnalysor
 {
     public partial class Form1 : Form
     {
+
+        DataHelper dh = new DataHelper();
+
         public Form1()
         {
             InitializeComponent();
         }
 
+        private void Form1_Load(object sender, EventArgs e)
+        {
+            FolderPath.Text = Properties.Settings.Default.FolderPath;
+            BackUpFolderPath.Text = Properties.Settings.Default.BackUpFolderPath;
+        }
+
         private void StartWatch_Click(object sender, EventArgs e)
         {
             XmlWatcher.Path = FolderPath.Text;
             XmlWatcher.Filter = "*.xml";
             XmlWatcher.Created += new FileSystemEventHandler(XmlWatcher_Created);
             XmlWatcher.EnableRaisingEvents = true;
+            Properties.Settings.Default.FolderPath = FolderPath.Text;
+            Properties.Settings.Default.BackUpFolderPath = BackUpFolderPath.Text;
+            Properties.Settings.Default.Save();
+            OperateResult.AppendText("开始执行监控\n");
         }
 
         private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
@@ -33,28 +47,53 @@ namespace UAS_XmlAnalysor
                     }
                     System.Threading.Thread.Sleep(500);
                 }
-                catch (Exception ex)
+                catch (Exception)
                 {
-                    Console.WriteLine(string.Format("Output file {0} not yet ready ({1})", e.Name, ex.Message));
                 }
             }
             XmlReader myReader = XmlReader.Create(FolderPath.Text + @"\" + e.Name);
+            string sncode = e.Name.Split('.')[0];
+            OperateResult.AppendText("读取文件" + e.Name + "\n");
+            List<string> name = new List<string>();
+            List<string> result = new List<string>();
+            int name_or_result = 0;
             while (myReader.Read())
             {
-                if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "test")
+                if (myReader.NodeType == XmlNodeType.Text)
                 {
-                    for (int i = 0; i < myReader.AttributeCount; i++)
+                    if (name_or_result % 2 == 0)
+                    {
+                        name.Add(myReader.Value);
+                        name_or_result++;
+                    }
+                    else
                     {
-                        Console.WriteLine(myReader.GetAttribute(i));
+                        result.Add(myReader.Value);
+                        name_or_result++;
                     }
                 }
             }
+            string sql = "insert into STEPTESTDETAIL(std_id,std_sn,std_subclass1,std_testresult,std_date) values (STEPTESTDETAIL_seq.nextval,'" + sncode + "',:std_subclass1,:std_testresult,sysdate)";
+            dh.BatchInsert(sql, new string[] { "std_subclass1", "std_testresult" }, name.ToArray(), result.ToArray());
             myReader.Close();
+            FileInfo file = new FileInfo(FolderPath.Text + @"\" + e.Name);
+            if (file.Exists)
+            {
+                try
+                {
+                    file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name);
+                }
+                catch (Exception ex)
+                {
+                    Console.WriteLine(ex.Message);
+                }
+            }
         }
 
         private void StopWatch_Click(object sender, EventArgs e)
         {
             XmlWatcher.EnableRaisingEvents = false;
+            OperateResult.AppendText("停止执行监控\n");
         }
 
         private void Clean_Click(object sender, EventArgs e)
@@ -78,9 +117,15 @@ namespace UAS_XmlAnalysor
 
         }
 
-        private void Form1_Load(object sender, EventArgs e)
+        private void ChooseBackUpFolder_Click(object sender, EventArgs e)
         {
-
+            FolderBrowserDialog folder = new FolderBrowserDialog();
+            folder.Description = "选择备份文件夹";
+            DialogResult result = folder.ShowDialog();
+            if (result == DialogResult.OK)
+            {
+                BackUpFolderPath.Text = folder.SelectedPath;
+            }
         }
     }
 }