/// <summary> /// 新建选项 /// </summary> /// <param name="sender">事件发送者</param> /// <param name="e">事件参数</param> private void bAddField_Click(object sender, EventArgs e) { Node currentNode = guideTree.SelectedNode; if (currentNode != null) { int orderIndex = currentNode.Nodes.Count; string nodeName = string.Format("选项{0}", orderIndex + 1); AddNodeForm addNodeForm = new AddNodeForm("新建选项", nodeName, ""); if (addNodeForm.ShowDialog() == DialogResult.OK) { nodeName = addNodeForm.NodeName; string nodeDescription = addNodeForm.NodeDescription; string tabID = currentNode.Tag as string; string id = null; SqlTransaction transaction = null; try { OpenConnection(); transaction = conn.BeginTransaction(); SqlCommand cmd = conn.CreateCommand(); cmd.Transaction = transaction; string sqlString = string.Format("SELECT ID FROM {0} WHERE Name = '{1}'", fieldTableName, nodeName); cmd.CommandText = sqlString; object executeResult = cmd.ExecuteScalar(); if (executeResult == null) { sqlString = string.Format("INSERT INTO {0} (Name, Description, TabID, OrderIndex) VALUES ('{1}', '{2}', {3}, {4})", fieldTableName, nodeName, nodeDescription, tabID, orderIndex); cmd.CommandText = sqlString; cmd.ExecuteNonQuery(); sqlString = string.Format("SELECT ID FROM {0} WHERE Name = '{1}'", fieldTableName, nodeName); cmd.CommandText = sqlString; executeResult = cmd.ExecuteScalar(); id = executeResult.ToString(); } transaction.Commit(); } catch (SqlException ex) { if (transaction != null) { transaction.Rollback(); } PrintExceptionMessage(ex); } finally { CloseConnection(); } if (id != null) { Node newNode = new Node(); newNode.Text = nodeName; newNode.Tag = id; newNode.ContextMenu = bFieldMenu; currentNode.Nodes.Add(newNode); guideTree.SelectedNode = newNode; } } } }
/// <summary> /// 新建向导 /// </summary> /// <param name="sender">事件发送者</param> /// <param name="e">事件参数</param> private void bAddClass_Click(object sender, EventArgs e) { Node currentNode = guideTree.SelectedNode; if (currentNode != null) { string nodeName = string.Format("向导{0}", currentNode.Nodes.Count + 1); AddNodeForm addNodeForm = new AddNodeForm("新建向导", nodeName, ""); if (addNodeForm.ShowDialog() == DialogResult.OK) { nodeName = addNodeForm.NodeName; string nodeDescription = addNodeForm.NodeDescription; string id = null; SqlTransaction transaction = null; try { OpenConnection(); transaction = conn.BeginTransaction(); SqlCommand cmd = conn.CreateCommand(); cmd.Transaction = transaction; string sqlString = string.Format("SELECT ID FROM {0} WHERE Name = '{1}'", classTableName, nodeName); cmd.CommandText = sqlString; object executeResult = cmd.ExecuteScalar(); if (executeResult == null) { sqlString = string.Format("INSERT INTO {0} (Name, Description) VALUES ('{1}', '{2}')", classTableName, nodeName, nodeDescription); cmd.CommandText = sqlString; cmd.ExecuteNonQuery(); sqlString = string.Format("SELECT ID FROM {0} WHERE Name = '{1}'", classTableName, nodeName); cmd.CommandText = sqlString; executeResult = cmd.ExecuteScalar(); id = executeResult.ToString(); } transaction.Commit(); } catch (SqlException ex) { if (transaction != null) { transaction.Rollback(); } PrintExceptionMessage(ex); } finally { CloseConnection(); } if (id != null) { Node newNode = new Node(); newNode.Text = nodeName; newNode.Tag = id; newNode.ContextMenu = bClassMenu; currentNode.Nodes.Add(newNode); guideTree.SelectedNode = newNode; } } } }