我正在使用树视图系统,该系统从我们拥有的数据库加载信息。我试图找出如何根据数据库中的ID将子节点添加到父节点。
IE:主要配置文件的ID = 1检查子配置文件,如果ID匹配,则将其作为子项添加到主要配置文件中。我添加了下面使用的代码。我能够加载父节点没有问题,但我无法将父节点添加到父节点。应根据主要的sID是否与孩子的m_Sysid匹配然后添加孩子来添加孩子。
public void Main_Profile_Load()
{
string ConfigurationFile = @"C:\Profiles\cfg\verswitch.sdf";
SqlCeConnection conn = null;
try
{
using (conn = new SqlCeConnection("Data Source =" + ConfigurationFile + "; Password =****"))
{
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "select StoreID, StoreName, Children, ID from t_MainProfiles";
cmd.ExecuteNonQuery();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
string sID = reader[0].ToString().Trim();
string sName = reader[1].ToString().Trim();
string sChild = reader[2].ToString().Trim();
string sSysID = reader[3].ToString().Trim();
TreeNode parent = new TreeNode();
parent.Text = sName+ " - "+sID;
treeView1.Nodes.Add(parent);
}
reader.Close();
MessageBox.Show("Main Profiles Loaded. Retrieved ");
}
}
catch
{
MessageBox.Show("Unable to locate Database");
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
finally
{
conn.Close();
}
现在她是子配置文件加载的代码。
public void Sub_Profile_Load()
{
string ConfigurationFile = @"C:\Profiles\cfg\verswitch.sdf";
SqlCeConnection conn = null;
try
{
using (conn = new SqlCeConnection("Data Source =" + ConfigurationFile + "; Password =****"))
{
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "select StoreID, Name, m_ProfileID from t_SubProfiles where Enabled = 1";
cmd.ExecuteNonQuery();
var reader = cmd.ExecuteReader();
DataTable sprofile = new DataTable();
while (reader.Read())
{
DataRow row = sprofile.NewRow();
row["Storeid"] = (reader[0].ToString().Trim());
row["name"] = (reader[1].ToString().Trim());
row["mproID"] = (reader[2].ToString().Trim());
sprofile.Rows.Add(row);
}
reader.Close();
MessageBox.Show("Sub Profiles Loaded. Retrieved " + sprofile.Rows.Count);
}
}
catch
{
MessageBox.Show("Unable to locate Database ");
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
finally
{
conn.Close();
}
}
我根本不知道如何将它们组合在一起。