private void InsertMacroMicro(MacroCommand mac, Command cmd) { if (!cmd.IsMacroCommand) { SqlConnection conn = new SqlConnection(@"Data Source= WS-DEVNS-22;Initial Catalog=Command;Integrated Security=True"); //string command = "INSERT INTO MacroMicro (MacroCommandID, MicroCommandID) VALUES (@Val1, @Val2)"; try { conn.Open(); //SqlCommand sql = new SqlCommand(command, conn); SqlCommand sql = new SqlCommand("spCommandInsert", conn); sql.CommandType = CommandType.StoredProcedure; int?MacroID = mac.Id; int?MicroID = cmd.Id; sql.Parameters.AddWithValue("@MacroCommandID", MacroID); sql.Parameters.AddWithValue("@MicroCommandID", MicroID); sql.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert Error:"; msg += ex.Message; throw new Exception(msg); } finally { conn.Close(); } } else { SqlConnection conn = new SqlConnection(@"Data Source= WS-DEVNS-22;Initial Catalog=Command;Integrated Security=True"); //string command = "UPDATE MacroCommands SET ParentId = @Val1 WHERE MacroCommandId = @Val2"; try { conn.Open(); SqlCommand sql = new SqlCommand("spCommandUpdate", conn); sql.CommandType = CommandType.StoredProcedure; //SqlCommand sql = new SqlCommand(command, conn); int?MacroID = mac.Id; int?MacroChildId = cmd.Id; sql.Parameters.AddWithValue("@MacrocomandID1", MacroID); sql.Parameters.AddWithValue("@MacrocomandID2", MacroChildId); sql.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert Error:"; msg += ex.Message; throw new Exception(msg); } finally { conn.Close(); } } }
private void DeleteCommand(MacroCommand mac, Command cmd) { SqlConnection conn = new SqlConnection(@"Data Source= WS-DEVNS-22;Initial Catalog=Command;Integrated Security=True"); conn.Open(); SqlCommand sql = new SqlCommand("spCommandDelete", conn); sql.CommandType = CommandType.StoredProcedure; //string commanddelete = "delete top (1) from MacroMicro where MacroCommandID = @Val1 and MicroCommandID = @Val2"; //SqlCommand sql = new SqlCommand(commanddelete, conn); int?MacroID = mac.Id; int?MicroID = cmd.Id; sql.Parameters.AddWithValue("@MacroCommandId", MacroID); sql.Parameters.AddWithValue("@MicroCommandId", MicroID); int ret = sql.ExecuteNonQuery(); conn.Close(); }
private void Form1_Load(object sender, EventArgs e) { SqlConnection obj = new SqlConnection(@"Data Source=WS-DEVNS-22;Initial Catalog=Command;Integrated Security=True"); try { obj.Open(); SqlCommand objcommand = new SqlCommand("procMicroCommands", obj); objcommand.CommandType = CommandType.StoredProcedure; //SqlCommand objcommand = new SqlCommand("Select * from Microcommands", obj); SqlDataReader dr = objcommand.ExecuteReader(); while (dr.Read()) { int idxID = dr.GetOrdinal("MicroCommandID"); Command cmd = CreateMicroCommand(dr.GetInt32(idxID), dr["MicroCommandName"].ToString(), dr["MicroCommandType"].ToString(), dr["MicroParameter1"].ToString(), dr["MicroParameter2"].ToString(), dr["MicroParameter3"].ToString(), statusStrip1); microCommands.Add((int)cmd.Id, (MicroCommand)cmd); ListViewItem item = CommandToListViewItem(cmd); listView1.Items.Add(item); } } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert Error:"; msg += ex.Message; throw new Exception(msg); } finally { obj.Close(); } try { obj.Open(); SqlCommand objcommand2 = new SqlCommand("procMacroCommands", obj); objcommand2.CommandType = CommandType.StoredProcedure; //SqlCommand objcommand2 = new SqlCommand("Select * from Macrocommands", obj); SqlDataReader dr2 = objcommand2.ExecuteReader(); while (dr2.Read()) { int idxId = dr2.GetOrdinal("MacroCommandId"); int idxParentId = dr2.GetOrdinal("ParentId"); MacroCommand cmd = new MacroCommand(dr2["MacroCommandName"].ToString()); cmd.Id = dr2.GetInt32(idxId); cmd.ParentId = dr2.IsDBNull(idxParentId) ? (int?)null : dr2.GetInt32(idxParentId); macroCommands.Add((int)cmd.Id, cmd); } dr2.Close(); foreach (var cmd in macroCommands.Values) { if (cmd.ParentId.HasValue) { var parent = macroCommands[(int)cmd.ParentId]; parent.Add(cmd); } } SqlCommand objcommand1 = new SqlCommand("procMacroMicro", obj); objcommand1.CommandType = CommandType.StoredProcedure; //SqlCommand objcommand1 = new SqlCommand("select MacroCommandId, MicroCommandId from MacroMicro", obj); SqlDataReader dr1 = objcommand1.ExecuteReader(); while (dr1.Read()) { int idxMacroId = dr1.GetOrdinal("MacroCommandID"); int idxMicroId = dr1.GetOrdinal("MicroCommandId"); // gat actual ids int macroId = dr1.GetInt32(idxMacroId); int microId = dr1.GetInt32(idxMicroId); var macroCmd = macroCommands[macroId]; var microCmd = microCommands[microId]; macroCmd.Add(microCmd); } } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert Error:"; msg += ex.Message; throw new Exception(msg); } finally { obj.Close(); } // try to draw tree view DrawTreeView(); }