C# 点选treeview1的三级节点根据节点名称以及根节点名称作为查询条件读取sql的sop表中字段prd_cat、prd_name、sop_name、ver、sop_page的值并分别把查询到的prd_cat值给tbx_p_cat文本框ver值给tbx_ver文本框
以下是一个示例代码,演示如何根据TreeView1的三级节点名称以及根节点名称作为查询条件,读取SQL的SOP表中的字段值,并将查询到的值分别赋给文本框tbx_p_cat和tbx_ver:
private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (e.Node.Level == 2) // 只处理三级节点
{
// 获取根节点的名称
string rootNodeName = e.Node.Parent.Parent.Text;
// 获取三级节点的名称
string nodeName = e.Node.Text;
// 构建SQL查询语句
string sqlQuery = "SELECT prd_cat, ver FROM sop WHERE prd_name = @prdName AND sop_name = @sopName AND root_node = @rootNode";
// 创建并打开数据库连接
using (SqlConnection connection = new SqlConnection("YourConnectionString"))
{
connection.Open();
// 创建并执行SQL命令
using (SqlCommand command = new SqlCommand(sqlQuery, connection))
{
// 设置查询参数
command.Parameters.AddWithValue("@prdName", nodeName);
command.Parameters.AddWithValue("@sopName", rootNodeName);
command.Parameters.AddWithValue("@rootNode", rootNodeName);
// 执行查询并读取结果
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
// 读取查询结果的字段值
string prdCat = reader.GetString(0);
string ver = reader.GetString(1);
// 将字段值赋给文本框
tbx_p_cat.Text = prdCat;
tbx_ver.Text = ver;
}
}
}
}
}
}
请注意替换代码中的"YourConnectionString"为您的数据库连接字符串。此外,还需要根据实际的数据库表结构和字段名称进行调整
原文地址: http://www.cveoy.top/t/topic/ismn 著作权归作者所有。请勿转载和采集!