public static Fault findFaultByFaultID(int faultID)
 {
     SqlConnection conn = null;
     SqlCommand cmd = null;
     try
     {
         conn = new SqlConnection(conStr);
         conn.Open();
         cmd = conn.CreateCommand();
         cmd.CommandText = "select * from fault where fault_ID=@faultID";
         cmd.Parameters.AddWithValue("@faultID", faultID);
         SqlDataReader reader = cmd.ExecuteReader();
         while (reader.Read())
         {
             Fault fa = new Fault();
             fa.faultID = reader.GetInt32(reader.GetOrdinal("fault_ID"));
             fa.fault = reader.GetString(reader.GetOrdinal("fault"));
             string faultImgs = reader.GetString(reader.GetOrdinal("fault_img"));
             string[] fimgs = faultImgs.Split(',');
             foreach (string img in fimgs)
             {
                 fa.faultImgs.Add(img);
             }
             fa.faultKey = reader.GetString(reader.GetOrdinal("fault_key"));
             return fa;
         }
         return null;
     }
     finally
     {
         conn.Close();
     }
 }
 //    public void Insert(Operator op)
 //    {
 //       SqlHelper.AddUser(op);
 //       return;
 //    }
 //    public void Delete(Operator op)
 //    {
 //         SqlHelper.DeleteUser(op);
 //         return;
 //    }
 public void Update(Fault op)
 {
     SqlHelper.updateFault(op);
        return;
 }
 public static int deleteFault(Fault fault)
 {
     SqlConnection conn = null;
     SqlCommand cmd = null;
     try
     {
         conn = new SqlConnection(conStr);
         conn.Open();
         cmd = conn.CreateCommand();
         cmd.CommandText = "delete from fault where fault_ID=@faultID";
         cmd.Parameters.AddWithValue("@faultID", fault.faultID);
         return cmd.ExecuteNonQuery();
     }
     finally
     {
         conn.Close();
     }
 }
        public static int addFault(Fault fault)
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
            try
            {

                conn = new SqlConnection(conStr);
                conn.Open();
                cmd = conn.CreateCommand();

                if (findFaultByFaultID(fault.faultID) != null)
                    return -1;

                cmd.CommandText = "insert into fault (fault,fault_img,fault_key) values(@fault,@faultImg,@faultKey)";
                // cmd.Parameters.AddWithValue("@faultID", fault.faultID);
                cmd.Parameters.AddWithValue("@fault", fault.fault);
                String imgs = null;
                int i = 0;
                foreach (string im in fault.faultImgs)
                {
                    if (i > 0) { imgs = imgs + ","; }
                    i++;
                    imgs = imgs + im;
                }
                cmd.Parameters.AddWithValue("@faultImg", imgs);
                if (fault.faultKey.Equals(""))
                {
                    cmd.Parameters.AddWithValue("@faultKey", "null");
                }
                else
                {
                    cmd.Parameters.AddWithValue("@faultKey", fault.faultKey);
                }
                return cmd.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }
        public static int updateMessage(Message m)
        {
            Operator op = findAllByRecordID(m.recordID);

            m.solutionID = op.solution.solutionID;
            m.faultID = op.fault.faultID;

            Record r = new Record();
            r.recordID = m.recordID;
            r.carID = m.carID;
            r.time = m.time;
            r.worker = m.worker;
            r.faultID = m.faultID;
            r.solutionID = m.solutionID;
            updateRecord(r);

            Fault f = new Fault();
            f.faultID = m.faultID;
            f.fault = m.fault;
            f.faultKey = m.faultKey;
            f.faultImgs = op.fault.faultImgs;
            updateFault(f);

            Solution s = new Solution();
            s.solutionID = m.solutionID;
            s.solution = m.solution;
            s.runSolution = m.runSolution;
            s.criImgs = op.solution.criImgs;
            int i = 0;
            i = updateSolution(s);
            if (i != 0)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
 public static int updateFault(Fault fault)
 {
     SqlConnection conn = null;
     SqlCommand cmd = null;
     try
     {
         conn = new SqlConnection(conStr);
         conn.Open();
         cmd = conn.CreateCommand();
         cmd.CommandText = "update fault set fault=@fault,fault_img=@faultImg,fault_key=@faultKey where fault_ID=@faultID";
         cmd.Parameters.AddWithValue("@faultID", fault.faultID);
         cmd.Parameters.AddWithValue("@fault", fault.fault);
         String imgs = null;
         int i = 0;
         foreach (string im in fault.faultImgs)
         {
             if (i > 0) { imgs = imgs + ","; }
             i++;
             imgs = imgs + im;
         }
         cmd.Parameters.AddWithValue("@faultImg", imgs);
         cmd.Parameters.AddWithValue("@faultKey", fault.faultKey);
         return cmd.ExecuteNonQuery();
     }
     finally
     {
         conn.Close();
     }
 }
 public static List<Fault> getAllFault()
 {
     SqlConnection conn = null;
     SqlCommand cmd = null;
     try
     {
         conn = new SqlConnection(conStr);
         conn.Open();
         cmd = conn.CreateCommand();
         cmd.CommandText = "select * from fault";
         SqlDataReader reader = cmd.ExecuteReader();
         List<Fault> faultList = new List<Fault>();
         while (reader.Read())
         {
             Fault f = new Fault();
             f.faultID = reader.GetInt32(reader.GetOrdinal("fault_ID"));
             f.fault = reader.GetString(reader.GetOrdinal("fault"));
             string faultImgs = reader.GetString(reader.GetOrdinal("fault_img"));
             string[] fimgs = faultImgs.Split(',');
             foreach (string img in fimgs)
             {
                 f.faultImgs.Add(img);
             }
             f.faultKey = reader.GetString(reader.GetOrdinal("fault_key"));
             faultList.Add(f);
         }
         return faultList;
     }
     finally
     {
         conn.Close();
     }
 }