using System; using System.Xml; using System.Collections.Generic; using System.Collections; using System.Text; using System.Data; using System.IO; using MySql.Data.MySqlClient; using System.Windows.Forms; namespace SafeMobileLib { public class SMdb_accessmy { private MySqlConnection cnMY; private string m_server; private string m_dbname; private string m_user; private string m_password; public SMdb_accessmy() { } ~SMdb_accessmy() { CloseConnection(); } public void InitConnection(string p_server, string p_dbname, string p_user, string p_password, string p_port) { string MyConString = "server=" + p_server + ";" + "uid=" + p_user + ";" + "pwd=" + p_password + ";" + "database=" + p_dbname + ";" + "PORT="+p_port+";" + "charset=utf8;"+ "Pooling=false;"; //Console.WriteLine("InitConnection: " + MyConString); cnMY= new MySqlConnection(); cnMY.ConnectionString = MyConString; m_server = p_server; m_dbname = p_dbname; m_user = p_user; m_password = p_password; try { cnMY.Open(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: InitConnection " + o.Message.ToString()); if (cnMY != null) { cnMY.Close(); Console.WriteLine("CLOSE CONNECTION"); } throw new ArgumentException(o.Message.ToString()); } } public Int32 get_subscriber_count() { MySqlCommand cmd; MySqlDataReader dr; int count = 0; try { cmd = new MySqlCommand("SELECT COUNT(*) from subscriber", cnMY); try { dr = cmd.ExecuteReader(); if (dr.Read()) count = dr.GetInt32(0); //Clean up. dr.Close(); } catch (MySqlException o) { Console.Write("File: SMdb_access.cs Meth: get_IMEI : "); Console.WriteLine(o.Message.ToString()); } } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: get_IMEI : " + o.Message.ToString()); throw new ArgumentException(o.Message.ToString()); } return count; } public void InsertXML(string p_message) { XmlDocument docXML = new XmlDocument(); docXML.LoadXml(p_message); XmlElement leRoot = docXML.DocumentElement; XmlAttributeCollection attr = leRoot.Attributes; string id = attr["id"].InnerXml; switch (id) { case "100001": insertSchedule(p_message);break; case "100010": insertZone(p_message); break; case "100011": insertDIevnt(p_message); break; case "100100": insertIDLE(p_message); break; case "100111": insertRFID(p_message); break; case "101010": insertPoll(p_message); break; case "110000": insertConnNotice(p_message); break; case "110010": insertStopEvnt(p_message); break; default: insertUnknown(p_message); break; } } private uint DateTo70Format(DateTime param) { long nOfSeconds; Console.WriteLine("DateTo70Format param=" + param); System.DateTime dt70 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); TimeSpan span = param - dt70; nOfSeconds = (long)span.TotalSeconds; return ((uint)nOfSeconds); } private void insertSchedule(string p_message) { try { MySqlCommand cmd; XmlDocument docXML = new XmlDocument(); docXML.LoadXml(p_message); XmlElement leRoot = docXML.DocumentElement; XmlAttributeCollection attr = leRoot.Attributes; string query= "call insert_msg_pos('"+ attr["subscriber"].InnerXml+"','"+ attr["latitude"].InnerXml+"','"+ attr["longitude"].InnerXml+"','"+ attr["speed"].InnerXml+"','"+ attr["dgr"].InnerXml + "','" + attr["di"].InnerXml+"','"+ attr["do"].InnerXml+"','"+ attr["time"].InnerXml+"','0',null);"; Console.WriteLine(query); cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: insertSchedule " + o.Message.ToString()); throw new ArgumentException(o.Message.ToString()); } } private void insertStopEvnt(string p_message) { try { MySqlCommand cmd; XmlDocument docXML = new XmlDocument(); docXML.LoadXml(p_message); XmlElement leRoot = docXML.DocumentElement; XmlAttributeCollection attr = leRoot.Attributes; string query = "call insert_msg_stop('" + attr["subscriber"].InnerXml + "','" + attr["latitude"].InnerXml + "','" + attr["longitude"].InnerXml + "','" + attr["speed"].InnerXml + "','" + attr["dgr"].InnerXml + "','" + attr["di"].InnerXml + "','" + attr["do"].InnerXml + "','" + attr["time"].InnerXml + "');"; Console.WriteLine(query); cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: insertStopEvnt " + o.Message.ToString()); throw new ArgumentException(o.Message.ToString()); } } private void insertZone(string p_message) { try { MySqlCommand cmd; XmlDocument docXML = new XmlDocument(); docXML.LoadXml(p_message); XmlElement leRoot = docXML.DocumentElement; XmlAttributeCollection attr = leRoot.Attributes; string query = "call insert_msg_zone('" + attr["subscriber"].InnerXml + "','" + attr["latitude"].InnerXml + "','" + attr["longitude"].InnerXml + "','" + attr["speed"].InnerXml + "','" + attr["dgr"].InnerXml + "','" + attr["di"].InnerXml + "','" + attr["do"].InnerXml + "','" + //attr["time"].InnerXml + "','" + attr["time"].InnerXml + "','1','" + //attr["zone_id"].InnerXml + "','" + attr["direction"].InnerXml + "');"; Console.WriteLine(query); cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: insertZone " + o.Message.ToString()); throw new ArgumentException(o.Message.ToString()); } } private void insertDIevnt(string p_message) { try { MySqlCommand cmd; XmlDocument docXML = new XmlDocument(); docXML.LoadXml(p_message); XmlElement leRoot = docXML.DocumentElement; XmlAttributeCollection attr = leRoot.Attributes; string query = "call insert_msg_di('" + attr["subscriber"].InnerXml + "','" + attr["latitude"].InnerXml + "','" + attr["longitude"].InnerXml + "','" + attr["speed"].InnerXml + "','" + attr["dgr"].InnerXml + "','" + attr["di"].InnerXml + "','" + attr["do"].InnerXml + "','" + attr["time"].InnerXml + "');"; Console.WriteLine(query); cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: insertDIevnt " + o.Message.ToString()); throw new ArgumentException(o.Message.ToString()); } } private void insertIDLE(string p_message) { try { MySqlCommand cmd; XmlDocument docXML = new XmlDocument(); docXML.LoadXml(p_message); XmlElement leRoot = docXML.DocumentElement; XmlAttributeCollection attr = leRoot.Attributes; string query = "call insert_msg_idle('" + attr["subscriber"].InnerXml + "','" + attr["latitude"].InnerXml + "','" + attr["longitude"].InnerXml + "','" + attr["speed"].InnerXml + "','" + attr["dgr"].InnerXml + "','" + attr["di"].InnerXml + "','" + attr["do"].InnerXml + "','" + attr["time"].InnerXml + "','" + attr["ai5"].InnerXml + "');"; //idle time is stored in ai5 value Console.WriteLine(query); cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: insertIDLE " + o.Message.ToString()); throw new ArgumentException(o.Message.ToString()); } } private void insertRFID(string p_message) { try { MySqlCommand cmd; XmlDocument docXML = new XmlDocument(); docXML.LoadXml(p_message); XmlElement leRoot = docXML.DocumentElement; XmlAttributeCollection attr = leRoot.Attributes; string query = "call insert_msg_rfid('" + attr["subscriber"].InnerXml + "','" + attr["latitude"].InnerXml + "','" + attr["longitude"].InnerXml + "','" + attr["speed"].InnerXml + "','" + attr["dgr"].InnerXml + "','" + attr["di"].InnerXml + "','" + attr["do"].InnerXml + "','" + attr["time"].InnerXml + "','" + attr["RFID"].InnerXml + "');"; //idle time is stored in ai5 value Console.WriteLine(query); cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: insertIDLE " + o.Message.ToString()); throw new ArgumentException(o.Message.ToString()); } } private void insertPoll(string p_message) { try { MySqlCommand cmd; XmlDocument docXML = new XmlDocument(); docXML.LoadXml(p_message); XmlElement leRoot = docXML.DocumentElement; XmlAttributeCollection attr = leRoot.Attributes; string query = "call insert_msg_poll('" + attr["subscriber"].InnerXml + "','" + attr["latitude"].InnerXml + "','" + attr["longitude"].InnerXml + "','" + attr["speed"].InnerXml + "','" + attr["dgr"].InnerXml + "','" + attr["di"].InnerXml + "','" + attr["do"].InnerXml + "','" + attr["time"].InnerXml + "','" + attr["POLL_ID"].InnerXml + "');"; Console.WriteLine(query); cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: insertPoll " + o.Message.ToString()); throw new ArgumentException(o.Message.ToString()); } } private void insertConnNotice(string p_message) { //TODO: insertConnNotice } private void insertUnknown(string p_message) { //TODO: insertUnknown } public ArrayList get_all_IMEI(int gateway_id) { ArrayList ret = new ArrayList(); MySqlCommand cmd; MySqlDataReader dr; String Tmp = "(no imei)"; try { cmd = new MySqlCommand("SELECT s.imei from subscriber s, subs_gateway g where s.sc_id=g.sc_id and g.gateway_id="+gateway_id+" ORDER BY s.imei", cnMY); try { dr = cmd.ExecuteReader(); while (dr.Read()) { Tmp = dr.GetString(0); ret.Add(Tmp); } dr.Close(); } catch (MySqlException o) { Console.Write("File: SMdb_access.cs Meth: get_IMEI : "); Console.WriteLine(o.Message.ToString()); } } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: get_IMEI : "+o.Message.ToString()); throw new ArgumentException(o.Message.ToString()); } return ret; } public void updateStatus(string p_IMEI, int status, int di) { Boolean insertIMEI = false; MySqlCommand cmd; int stat = get_status(p_IMEI); if (stat == -1) { try { string query = "INSERT INTO mototurbo VALUES('"+p_IMEI+"',"+status+"," + di +")"; Console.WriteLine(query); cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); query = "INSERT INTO logmototurbo VALUES('" + p_IMEI + "'," + DateTo70Format(DateTime.Now.ToUniversalTime())+ "," + status + ",null)"; Console.WriteLine(query); cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: INSERT STATUS " + o.Message.ToString()); throw new ArgumentException(o.Message.ToString()); } } else if (stat != status) { try { string query = "UPDATE mototurbo SET status =" + status + " where imei='" + p_IMEI + "'"; Console.WriteLine(query); cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); query = "INSERT INTO logmototurbo VALUES('" + p_IMEI + "'," + DateTo70Format(DateTime.Now.ToUniversalTime()) + "," + status + ",null)"; Console.WriteLine(query); cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: UPDATE STATUS " + o.Message.ToString()); insertIMEI = true; } } } public int get_status(String IMEI) { int status = -1; MySqlCommand cmd; MySqlDataReader dr; try { cmd = new MySqlCommand("SELECT status from mototurbo where imei='"+IMEI+"'", cnMY); try { dr = cmd.ExecuteReader(); while (dr.Read()) { status = dr.GetInt32(0); } dr.Close(); } catch (MySqlException o) { Console.Write("File: SMdb_access.cs Meth: get_IMEI : "); Console.WriteLine(o.Message.ToString()); } } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: get_IMEI : " + o.Message.ToString()); throw new ArgumentException(o.Message.ToString()); } return status; } public ArrayList get_sms_todeliver(int gateway_id) { ArrayList ret = new ArrayList(); MySqlCommand cmd; MySqlDataReader dr; smsmessage obj; String Tmp = "(no imei)"; try { cmd = new MySqlCommand("SELECT s.imei , m.timeGMT, m.idx, m.mess, s.sc_id from sms m, subscriber s, subs_gateway g where m.status=0 and s.sc_id=g.sc_id and g.gateway_id=" + gateway_id + " and s.sc_id=m.sc_id_dest ORDER BY m.timeGMT", cnMY); try { dr = cmd.ExecuteReader(); while (dr.Read()) { obj = new smsmessage(); obj.imei_dest = dr.GetString(0); obj.timeGMT = dr.GetUInt32(1); obj.idx = dr.GetInt32(2); obj.mess = dr.GetString(3); obj.sc_id_dest = dr.GetInt32(4); obj.status = 0; ret.Add(obj); } dr.Close(); } catch (MySqlException o) { Console.Write("File: SMdb_access.cs Meth: get_IMEI : "); Console.WriteLine(o.Message.ToString()); } } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: get_IMEI : " + o.Message.ToString()); throw new ArgumentException(o.Message.ToString()); } return ret; } public void update_sms_delivered(int idxmess, int status) { // status = 1 : GW read the msg and delivered it // status = 2 : GW received the confirm from radio unit MySqlCommand cmd; try { string query = "UPDATE sms SET status=" + status + " where idx=" + idxmess; Console.WriteLine(query); cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: UPDATE STATUS " + o.Message.ToString()); } } public void insert_sms_received(String imei, string mess, int type, string emailaddr) { MySqlCommand cmd; MySqlDataReader dr; Int32 sc_id=0; try { cmd = new MySqlCommand("SELECT sc_id from subscriber where imei='"+imei+"'", cnMY); try { dr = cmd.ExecuteReader(); while (dr.Read()) { sc_id = dr.GetInt32(0); } dr.Close(); } catch (MySqlException o) { Console.Write("File: SMdb_access.cs Meth: get_IMEI : "); Console.WriteLine(o.Message.ToString()); } } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: get_IMEI : " + o.Message.ToString()); throw new ArgumentException(o.Message.ToString()); } try { string query = "INSERT INTO sms VALUES(" + DateTo70Format(DateTime.Now.ToUniversalTime())+","+sc_id+",0,'"+mess+"',"+type+",null,'')"; if (type == 7) query = "INSERT INTO sms VALUES(" + DateTo70Format(DateTime.Now.ToUniversalTime()) + ",0," + sc_id + ",'" + mess + "'," + type + ",null,'" + emailaddr + "')"; Console.WriteLine(query); cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: UPDATE STATUS " + o.Message.ToString()); } } public String ReadTimeLimit(String Code) { String Retur = ""; try { MySqlCommand cmd; MySqlDataReader dr; cmd = new MySqlCommand("SELECT wait_name from run_wait where veh_name='" + Code + "'", cnMY); dr = cmd.ExecuteReader(); if (dr.Read()) { Retur = dr.GetString(0); } dr.Close(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: ReadTimeLimit " + o.Message.ToString()); } return Retur; } public void InsertTimeLimit(String Code, String Time) { MySqlCommand cmd; string query = ""; try { query = "DELETE from run_wait where veh_name='" + Code + "'"; cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); query = "INSERT INTO run_wait VALUES('" + Code + "'," + Time + ")"; cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: run_wait " + o.Message.ToString()); throw new ArgumentException(o.Message.ToString()); } } public void insert_sms_sent2email(String imei, String mess, String email) { MySqlCommand cmd; MySqlDataReader dr; Int32 sc_id=0; try { cmd = new MySqlCommand("SELECT sc_id from subscriber where imei='" + imei + "'", cnMY); try { dr = cmd.ExecuteReader(); while (dr.Read()) { sc_id = dr.GetInt32(0); } dr.Close(); } catch (MySqlException o) { Console.Write("File: SMdb_access.cs Meth: get_IMEI : "); Console.WriteLine(o.Message.ToString()); } } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: get_IMEI : " + o.Message.ToString()); throw new ArgumentException(o.Message.ToString()); } try { string query = "INSERT INTO sms VALUES(" + DateTo70Format(DateTime.Now.ToUniversalTime()) + "," + sc_id + ",0,'" + mess + "',5,null,'"+email+"')"; Console.WriteLine(query); cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: UPDATE STATUS " + o.Message.ToString()); } } public void CloseConnection() { if (cnMY != null) cnMY.Close(); } public void updateDi(string p_IMEI, int di) { MySqlCommand cmd; try { string query = "UPDATE mototurbo SET di =" + di + " where imei='" + p_IMEI + "'"; Console.WriteLine(query); cmd = new MySqlCommand(query, cnMY); cmd.ExecuteNonQuery(); } catch (MySqlException o) { Console.WriteLine("File: SMdb_access.cs Meth: UPDATE STATUS " + o.Message.ToString()); } } public Boolean addVehicleToDb(String _imei, String _user, String _name, String _serialNumber) { MySqlCommand cmd; MySqlDataReader Reader; try { cmd = new MySqlCommand("SELECT count(imei) FROM subscriber where imei='" + _imei + "'", cnMY); object temp = cmd.ExecuteScalar(); if (temp.ToString() == "0") { string id_subscriber = "", id_vehicle = "", id_user = ""; cmd = new MySqlCommand("INSERT INTO vehicle VALUES(null,'" + _serialNumber + "' ,'" + _name + "',1,0)", cnMY); cmd.ExecuteNonQuery(); cmd = new MySqlCommand("INSERT INTO subscriber VALUES(null,'" + _imei + "')", cnMY); cmd.ExecuteNonQuery(); //get vehicle id cmd = new MySqlCommand("SELECT max(id) FROM vehicle ", cnMY); Reader = cmd.ExecuteReader(); while (Reader.Read()) { id_vehicle = Reader.GetInt32(0).ToString(); } Reader.Close(); //get subscriber id cmd = new MySqlCommand("SELECT max(sc_id) FROM subscriber ", cnMY); Reader = cmd.ExecuteReader(); while (Reader.Read()) { id_subscriber = Reader.GetInt32(0).ToString(); } Reader.Close(); //get user id cmd = new MySqlCommand("SELECT userId FROM users Where login ='" + _user + "'", cnMY); Reader = cmd.ExecuteReader(); while (Reader.Read()) { id_user = Reader.GetInt32(0).ToString(); } Reader.Close(); if (id_vehicle.Length > 0 && id_subscriber.Length > 0 && id_user.Length > 0) { cmd = new MySqlCommand("INSERT INTO vehicle_user VALUES(" + id_vehicle + "," + id_user + ",'2007-01-01')", cnMY); cmd.ExecuteNonQuery(); cmd = new MySqlCommand("INSERT INTO subscriber_history VALUES(" + id_subscriber + "," + id_vehicle + ",'2007-01-01',null)", cnMY); cmd.ExecuteNonQuery(); cmd = new MySqlCommand("DELETE FROM lastpos where imei='" + _imei + "'", cnMY); cmd.ExecuteNonQuery(); cmd = new MySqlCommand("INSERT INTO lastpos VALUES('" + _imei + "',1,42.06054,-88.02397,0,0,0,0,0)", cnMY); cmd.ExecuteNonQuery(); cmd = new MySqlCommand("INSERT INTO subs_gateway VALUES(" + id_subscriber + "," + "1" + ")", cnMY); cmd.ExecuteNonQuery(); } MessageBox.Show("Vehicle " + _name + " added !", "successfuly!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { MessageBox.Show("There allready is a vehicle with IMEI : " + _imei + " in db ", "error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } catch (MySqlException ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); MessageBox.Show("MySql connection Failed1 !!!!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } return true; } public void deleteVehicle(String _imei) { MySqlCommand cmd; MySqlDataReader Reader; try { String scid = "0"; String vehid = "0"; String vehname = "0"; cmd = new MySqlCommand("SELECT count(imei) FROM subscriber where imei='" + _imei + "'", cnMY); object temp = cmd.ExecuteScalar(); if (temp.ToString() != "0") { cmd = new MySqlCommand("DELETE FROM lastpos where imei='" + _imei + "'", cnMY); cmd.ExecuteNonQuery(); cmd = new MySqlCommand("SELECT sc_id FROM subscriber where imei='" + _imei + "'", cnMY); Reader = cmd.ExecuteReader(); while (Reader.Read()) { scid = Reader.GetInt32(0).ToString(); } Reader.Close(); cmd = new MySqlCommand("SELECT veh_id FROM subscriber_history where sc_id=" + scid, cnMY); Reader = cmd.ExecuteReader(); while (Reader.Read()) { vehid = Reader.GetInt32(0).ToString(); } Reader.Close(); cmd = new MySqlCommand("SELECT name FROM vehicle where id=" + vehid, cnMY); Reader = cmd.ExecuteReader(); while (Reader.Read()) { vehname = Reader.GetString(0); } Reader.Close(); cmd = new MySqlCommand("DELETE FROM didocode where vehName='" + vehname + "'", cnMY); cmd.ExecuteNonQuery(); cmd = new MySqlCommand("DELETE FROM vehicle where id=" + vehid, cnMY); cmd.ExecuteNonQuery(); cmd = new MySqlCommand("DELETE FROM vehicle_user where veh_id=" + vehid, cnMY); cmd.ExecuteNonQuery(); cmd = new MySqlCommand("DELETE FROM subscriber_history where veh_id=" + vehid, cnMY); cmd.ExecuteNonQuery(); cmd = new MySqlCommand("DELETE FROM subscriber where sc_id=" + scid, cnMY); cmd.ExecuteNonQuery(); cmd = new MySqlCommand("DELETE FROM subs_gateway where sc_id=" + scid, cnMY); cmd.ExecuteNonQuery(); cmd = new MySqlCommand("SELECT evnt_id FROM messages where evnt_id<>0 and sc_id=" + scid, cnMY); Reader = cmd.ExecuteReader(); while (Reader.Read()) { cmd = new MySqlCommand("DELETE FROM events where eventID=" + Reader.GetString(0), cnMY); cmd.ExecuteNonQuery(); } Reader.Close(); cmd = new MySqlCommand("DELETE FROM messages where sc_id=" + scid, cnMY); cmd.ExecuteNonQuery(); try { cmd = new MySqlCommand("DELETE FROM subs_gateway where sc_id=" + scid, cnMY); cmd.ExecuteNonQuery(); } catch { } MessageBox.Show("Delete IMEI finish."); } else { MessageBox.Show("Vehicle Not in db", "error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (MySqlException ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); MessageBox.Show("MySql connection Failed2 !!!!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } public void editVehicle(String _id, String _name) { MySqlCommand cmd; try { cmd = new MySqlCommand("select count(id) from vehicle where id=\'" + _id + "\'", cnMY); object temp = cmd.ExecuteScalar(); if (Convert.ToInt32(temp) > 0) { cmd = new MySqlCommand("UPDATE vehicle SET name='" + _name + "' where id=" + _id, cnMY); cmd.ExecuteNonQuery(); MessageBox.Show("Vehicle edited!", "successfuly!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { MessageBox.Show("Vehicle Not found in db", "error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (MySqlException ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); MessageBox.Show("MySql connection Failed3 !!!!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } public ArrayList getAllVehicles() { ArrayList vehList = new ArrayList(); MySqlCommand cmd; MySqlDataReader Reader; cmd = new MySqlCommand("SELECT veh.id,sub.imei,veh.lp,veh.name" + " FROM vehicle as veh" + " INNER JOIN (subscriber_history as sh,subscriber as sub)" + " on ( veh.id = sh.veh_id and sh.sc_id = sub.sc_id)" + " Group by sub.imei" + " ORDER BY sub.imei",cnMY); try { Reader = cmd.ExecuteReader(); while (Reader.Read()) { Vehicles veh = new Vehicles(Convert.ToInt32(Reader.GetValue(0)), Reader.GetValue(1).ToString(), Reader.GetValue(2).ToString(), Reader.GetValue(3).ToString(), ""); vehList.Add(veh); } } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); MessageBox.Show("MySql connection Failed4 !!!!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return vehList; } public ArrayList getAllUsersForCurrentVehicle(String _id) { ArrayList userList = new ArrayList(); MySqlCommand cmd; MySqlDataReader Reader; cmd = new MySqlCommand("SELECT firstName, lastName, login, password,userId FROM users as u" + " inner join( vehicle_user as vh)" + " on(vh.user_id=u.userID)" + " where vh.veh_id =" + _id.ToString() + " GROUP BY u.userId" , cnMY); try { Reader = cmd.ExecuteReader(); while (Reader.Read()) { User usr = new User(Reader.GetValue(0).ToString(), Reader.GetValue(1).ToString(), Reader.GetValue(2).ToString(), Reader.GetValue(3).ToString(), Reader.GetInt32(4)); userList.Add(usr); } } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); MessageBox.Show("MySql connection Failed5 !!!!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return userList; } //get all users that ar not assigned to current vehicle public ArrayList getAllOtherUsersForCurrentVehicle(String _id) { ArrayList userList = new ArrayList(); MySqlCommand cmd; MySqlDataReader Reader; cmd = new MySqlCommand("SELECT firstName, lastName, login, password,userId FROM users as u" + " WHERE u.userID NOT IN " + " (SELECT user_id FROM vehicle_user " + " WHERE veh_id =" + _id + ")" , cnMY); try { Reader = cmd.ExecuteReader(); while (Reader.Read()) { User usr = new User(Reader.GetValue(0).ToString(), Reader.GetValue(1).ToString(), Reader.GetValue(2).ToString(), Reader.GetValue(3).ToString(), Convert.ToInt32(Reader.GetValue(4))); userList.Add(usr); } } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); MessageBox.Show("MySql connection Failed6 !!!!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return userList; } public void addUserToDb(String _firstName, String _lastname, String _userName, String _password) { MySqlCommand cmd; try { cmd = new MySqlCommand("select count(login) from users where login=\'" + _userName + "\'", cnMY); object temp = cmd.ExecuteScalar(); if (temp.ToString() == "0") { cmd = new MySqlCommand("INSERT INTO users VALUES(null,'" + _firstName + "','" + _lastname + "','" + _userName + "','" + _password + "')", cnMY); cmd.ExecuteNonQuery(); MessageBox.Show("user added !", "successfuly!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { MessageBox.Show("User allready in db or some fields are empty", "error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (MySqlException ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); MessageBox.Show("MySql connection Failed !!!!-1", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } public void updateUserToDb(String _firstName, String _lastname, String _userName, String _password) { MySqlCommand cmd; try { cmd = new MySqlCommand("select count(login) from users where login=\'" + _userName + "\'", cnMY); object temp = cmd.ExecuteScalar(); if (Convert.ToInt32(temp) > 0) { cmd = new MySqlCommand("UPDATE users SET firstName='" + _firstName + "',lastName='" + _lastname + "',login='" + _userName + "',password='" + _password + "' WHERE login='" + _userName + "'", cnMY); cmd.ExecuteNonQuery(); MessageBox.Show("user edited!", "successfuly!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { MessageBox.Show("User Not found in db", "error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (MySqlException ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); MessageBox.Show("MySql connection Failed !!!!0", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } public void deleteUser(string userName) { MySqlCommand cmd; try { cmd = new MySqlCommand("select count(login) from users where login=\'" + userName + "\'", cnMY); object temp = cmd.ExecuteScalar(); if (temp.ToString() != "0") { cmd = new MySqlCommand("DELETE FROM users where login='" + userName + "'", cnMY); cmd.ExecuteNonQuery(); MessageBox.Show("user Deleted !", "successfuly!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { MessageBox.Show("User Not found in DB", "error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (MySqlException ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); MessageBox.Show("MySql connection Failed !!!!1", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //get all users public ArrayList getAllUsers() { ArrayList userList = new ArrayList(); MySqlCommand cmd; MySqlDataReader Reader; cmd = new MySqlCommand("SELECT firstName, lastName, login, password,userId FROM users", cnMY); try { Reader = cmd.ExecuteReader(); while (Reader.Read()) { User usr = new User(Reader.GetValue(0).ToString(), Reader.GetValue(1).ToString(), Reader.GetValue(2).ToString(), Reader.GetValue(3).ToString(), Convert.ToInt32(Reader.GetValue(4))); userList.Add(usr); } } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); MessageBox.Show("MySql connection Failed !!!!2", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return userList; } //get all vehicles for current user public ArrayList getAllVehiclesForCurrentUser(String _id) { ArrayList vehList = new ArrayList(); MySqlCommand cmd; MySqlDataReader Reader; cmd = new MySqlCommand("SELECT veh.id,sub.imei,veh.lp,veh.name" + " FROM vehicle as veh" + " INNER JOIN (subscriber_history as sh,subscriber as sub)" + " on ( veh.id = sh.veh_id and sh.sc_id = sub.sc_id)" + " inner join( vehicle_user as vh)" + " on(vh.veh_id=veh.id)" + " Where vh.user_id=" + _id.ToString() + " Group by sub.imei" + " ORDER BY sub.imei", cnMY); try { Reader = cmd.ExecuteReader(); while (Reader.Read()) { Vehicles veh = new Vehicles(Convert.ToInt32(Reader.GetValue(0)), Reader.GetValue(1).ToString(), Reader.GetValue(2).ToString(), Reader.GetValue(3).ToString(), ""); vehList.Add(veh); } } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); MessageBox.Show("MySql connection Failed !!!!3", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return vehList; } //get all vehicles that ar not assigned to current user public ArrayList getAllOtherVehiclesForCurrentUser(String _id) { ArrayList vehList = new ArrayList(); MySqlCommand cmd; MySqlDataReader Reader; cmd = new MySqlCommand("SELECT veh.id,sub.imei,veh.lp,veh.name" + " FROM vehicle as veh" + " INNER JOIN (subscriber_history as sh,subscriber as sub)" + " on ( veh.id = sh.veh_id and sh.sc_id = sub.sc_id)" + " LEFT join( vehicle_user as vh)" + " on(vh.veh_id=veh.id)" + " WHERE veh.id NOT IN " + " (SELECT veh_id FROM vehicle_user " + " WHERE user_id =" + _id.ToString() + ")" + " Group by sub.imei" + " ORDER BY sub.imei", cnMY); try { Reader = cmd.ExecuteReader(); while (Reader.Read()) { Vehicles veh = new Vehicles(Convert.ToInt32(Reader.GetValue(0)), Reader.GetValue(1).ToString(), Reader.GetValue(2).ToString(), Reader.GetValue(3).ToString(), ""); vehList.Add(veh); } } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); MessageBox.Show("MySql connection Failed !!!!4", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return vehList; } // add connection between user and vehicle public void addVehicleUserConnection(string vehId, string userId) { MySqlCommand cmd; try { cmd = new MySqlCommand("INSERT INTO vehicle_user VALUES(" + vehId + "," + userId + ",'2007-01-01')", cnMY); cmd.ExecuteNonQuery(); } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); MessageBox.Show("MySql connection Failed !!!!5", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //remove connection between user and vehicle public void removeVehicleUserConnection(string vehId, string userId) { MySqlCommand cmd; try { cmd = new MySqlCommand("DELETE FROM vehicle_user WHERE veh_id ='" + vehId + "' and user_id='" + userId + "'", cnMY); cmd.ExecuteNonQuery(); } catch (Exception ee) { Console.WriteLine(ee.Message + " " + ee.StackTrace + " " + ee.Source); MessageBox.Show("MySql connection Failed !!!!6", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }