private void DeleteSignals(int PlcID) { DBLite db = new DBLite("delete from PLC_Signal where PLC=@id"); db.AddParameter("id", PlcID, DbType.Int32); db.Exec(); }
private void LoadPlc() { DBLite db = new DBLite("select Name, Desc, IP, Rack, Slot, Type from PLC where ID=@id"); db.AddParameter("id", _id, DbType.Int32); using (SQLiteDataReader dr = db.ExecReader()) { if (dr.Read()) { _name = dr.GetString(dr.GetOrdinal("Name")); _ip = dr.GetString(dr.GetOrdinal("IP")); _rack = dr.GetInt32(dr.GetOrdinal("Rack")); _slot = dr.GetInt32(dr.GetOrdinal("Slot")); switch (dr.GetString(dr.GetOrdinal("Type"))) { case "s7-1500": _type = Plc.S7Type.S71500; break; case "s7-1200": _type = Plc.S7Type.S71200; break; case "s7-400": _type = Plc.S7Type.S7400; break; case "s7-300": _type = Plc.S7Type.S7300; break; } } } }
private void smazatToolStripMenuItem_Click(object sender, EventArgs e) { if (MessageBox.Show("Delete PLC?", "Delete PLC", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int id = Convert.ToInt32(treeView1.SelectedNode.Tag); if (id >= 0) { // Signals DBLite dbsignals = new DBLite("delete from PLC_Signal where PLC=@id"); dbsignals.AddParameter("id", id, DbType.Int32); dbsignals.Exec(); // PLC DBLite db = new DBLite("delete from PLC where ID=@id"); db.AddParameter("id", id, DbType.Int32); db.Exec(); // Close CloseEdit(); } LoadTree(); }
private int GetPlcID() { int id = 0; DBLite db = new DBLite("select ID from PLC where Name like @name and IP like @ip"); db.AddParameter("name", this._name, DbType.String); db.AddParameter("ip", this._ip, DbType.String); using (SQLiteDataReader dr = db.ExecReader()) { if (dr.Read()) { id = dr.GetInt32(0); } } return(id); }
private void btn_ok_Click(object sender, EventArgs e) { try { if (!string.IsNullOrEmpty(txt_ip.Text) && !string.IsNullOrEmpty(txt_name.Text)) { if (!Test()) { if (MessageBox.Show("Unable to connect to PLC, continue anyway?", "Unable to connect", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } } if (!Edit) { if (!PLCExists()) { DBLite db = new DBLite("insert into PLC values (null, @name, ifnull(@desc,''), @ip, @r, @s, @type)"); db.AddParameter("name", txt_name.Text, DbType.String); db.AddParameter("desc", txt_desc.Text, DbType.String); db.AddParameter("ip", txt_ip.Text, DbType.String); db.AddParameter("r", rack.Value, DbType.Int32); db.AddParameter("s", slot.Value, DbType.Int32); db.AddParameter("type", combo_typ.SelectedValue.ToString(), DbType.String); db.Exec(); this.DialogResult = DialogResult.OK; } else { MessageBox.Show("There is already PLC with this name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { if (!PLCExists()) { DBLite db = new DBLite("update PLC set Name=@name, Desc=@desc, IP=@ip, Rack=@r, Slot=@s, Type=@type where ID=@id "); db.AddParameter("name", txt_name.Text, DbType.String); db.AddParameter("desc", txt_desc.Text, DbType.String); db.AddParameter("ip", txt_ip.Text, DbType.String); db.AddParameter("r", rack.Value, DbType.Int32); db.AddParameter("s", slot.Value, DbType.Int32); db.AddParameter("type", combo_typ.SelectedValue.ToString(), DbType.String); db.AddParameter("id", ID, DbType.Int32); db.Exec(); this.DialogResult = DialogResult.OK; } else { MessageBox.Show("There is already PLC with this name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error while saving", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void SaveSignals(int PlcID) { try { foreach (DataGridViewRow row in dataGridView1.Rows) { string desc = ""; string repre = ""; string addr = ""; if (row.Cells["desc"].Value != null) { desc = row.Cells["desc"].Value.ToString(); } if (row.Cells["address"].Value != null) { addr = row.Cells["address"].Value.ToString(); } if (row.Cells["format"].Value != null) { repre = row.Cells["format"].Value.ToString(); } DBLite db = new DBLite("insert into PLC_Signal values (@id, @addr, @desc, @repre)"); db.AddParameter("id", this._id, DbType.Int32); db.AddParameter("addr", addr, DbType.String); db.AddParameter("desc", desc, DbType.String); db.AddParameter("repre", repre.ToLower(), DbType.String); db.Exec(); db = null; } MessageBox.Show("Successfully saved", "Saved"); }catch (Exception ex) { MessageBox.Show(ex.Message, "Save error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void LoadSignals() { // TODO: GetPlcID is not needed, ID is already known // TODO: Should use _id; int plcid = GetPlcID(); DBLite db = new DBLite("select Addr, Desc, repre from PLC_Signal where PLC=@id"); db.AddParameter("id", plcid, DbType.Int32); using (SQLiteDataReader dr = db.ExecReader()) { while (dr.Read()) { dataGridView1.Rows.Add(); int newrow = dataGridView1.Rows.Count - 2; string addr = ""; string desc = ""; string repre = ""; if (!dr["Addr"].Equals(DBNull.Value)) { addr = dr.GetString(dr.GetOrdinal("Addr")); } if (!dr["Desc"].Equals(DBNull.Value)) { desc = dr.GetString(dr.GetOrdinal("Desc")); } if (!dr["repre"].Equals(DBNull.Value)) { repre = dr.GetString(dr.GetOrdinal("repre")); } dataGridView1.Rows[newrow].Cells["address"].Value = string.IsNullOrEmpty(addr) ? null : addr; dataGridView1.Rows[newrow].Cells["desc"].Value = desc; if (!string.IsNullOrEmpty(addr)) { dataGridView1_CellEndEdit(dataGridView1, new DataGridViewCellEventArgs(dataGridView1.Rows[newrow].Cells["address"].ColumnIndex, dataGridView1.Rows[newrow].Index)); } dataGridView1.Rows[newrow].Cells["format"].Value = repre.ToUpper(); } dataGridView1.Update(); } }
private Boolean PLCExists() { string query = "select * from PLC where Name like @name"; if (Edit) { query = "select * from PLC where Name like @name and ID<>@id"; } DBLite db = new DBLite(query); db.AddParameter("name", txt_name.Text, DbType.String); if (Edit) { db.AddParameter("id", ID, DbType.Int32); } using (SQLiteDataReader dr = db.ExecReader()) { if (dr.HasRows) { return(true); } } return(false); }
private void combo_typ_SelectedIndexChanged(object sender, EventArgs e) { if (combo_typ.SelectedIndex >= 0) { DBLite db = new DBLite("select Def_Rack, Def_Slot from PLC_type where ID like @id"); db.AddParameter("id", combo_typ.SelectedValue, DbType.String); using (SQLiteDataReader dr = db.ExecReader()) { if (dr.Read()) { rack.Value = dr.GetValue(0).Equals(DBNull.Value) ? 0 : dr.GetInt32(0); slot.Value = dr.GetValue(1).Equals(DBNull.Value) ? 0 : dr.GetInt32(1); } } } }
private void LoadPLC() { DBLite db = new DBLite("SELECT Name,Desc,IP,Rack,Slot,Type FROM PLC where ID=@id"); db.AddParameter("id", ID, DbType.Int32); using (SQLiteDataReader dr = db.ExecReader()) { if (dr.Read()) { txt_name.Text = dr.GetString(dr.GetOrdinal("Name")); txt_ip.Text = dr.GetString(dr.GetOrdinal("IP")); txt_desc.Text = dr.GetValue(dr.GetOrdinal("Desc")) == DBNull.Value ? "" : dr.GetString(dr.GetOrdinal("Desc")); combo_typ.SelectedValue = dr.GetString(dr.GetOrdinal("Type")); rack.Value = dr.GetInt32(dr.GetOrdinal("Rack")); slot.Value = dr.GetInt32(dr.GetOrdinal("Slot")); } } }