怎么用TreeView绑定数据库里的省市县数据

怎么用TreeView绑定数据库里的省市县数据,第1张

数据表:

CREATE TABLE CateTable (

[ID] [int] IDENTITY (1, 1) NOT NULL ,

[CateName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,

[RootID] [int] NOT NULL ,

[ParentID] [int] NOT NULL

)

GO

存储过程:

CREATE PROCEDURE CateTable_GetList AS

BEGIN

Select

CateID,

CateName,

RootID,

ParentID

From

CateTable

Order By

CateID,RootID,ParentID

END

代码:

private void MainForm_Load(object sender, EventArgs e)

{

//从数据库中读取数据

SqlConnection con = new SqlConnection(ConnString.ConStr)

SqlCommand cmd = new SqlCommand("CateTable_GetList", con)

cmd.CommandType = CommandType.StoredProcedure

SqlDataAdapter sda = new SqlDataAdapter(cmd)

DataSet ds = new DataSet()

try

{

sda.Fill(ds)

}

catch

{

}

finally

{

cmd = null

con.Close()

}

//往TreeView中添加树节点

//添加根节点

TreeNode tn = new TreeNode()

tn.Text = "种类"

tn.Name = "0"//Name作为ID

tn.Tag = "0"//Tag作为RootID

tn.ImageIndex = 0

tn.SelectedImageIndex = 0

tv.Nodes.Add(tn)//该TreeView命名为tv

tv.SelectedNode = tv.TopNode

//把其他节点加上去

if (ds != null)

{

foreach (DataRow dr in ds.Tables[0].Rows)

{

tn = new TreeNode()

tn.Text = dr["CateName"].ToString()

tn.Name = dr["CateID"].ToString()//Name作为CateID

tn.Tag = dr["RootID"].ToString()//Tag作为RootID

tn.ImageIndex = 1

tn.SelectedImageIndex = 1

//判断是否为主节点

if (dr["CateID"].ToString() == dr["RootID"].ToString())

{

//主节点

tv.SelectedNode = tv.TopNode

}

else

{

//其他节点

if (tv.SelectedNode.Name != dr["ParentID"].ToString())

{

TreeNode[] tn_temp = tv.Nodes.Find(dr["ParentID"].ToString(), true)

if (tn_temp.Length >0)

{

tv.SelectedNode = tn_temp[0]

}

}

}

tv.SelectedNode.Nodes.Add(tn)

}

tv.ExpandAll()//展开TreeView

tv.SelectedNode = tv.TopNode

}

}

(1)要求查找”中国河北省“的所有县名。该如何查找,写出SQL语句

SELECT

县郡表.县郡名

FROM

国家表 JOIN 省市表 ON ( 国家表.国家编号 = 省市表.国家编号 )

JOIN 县郡表 ON ( 县郡表.省市编号 = 省市表.省市编号 )

WHERE

国家表.国名 = '中国' AND 省市表.省市名 = '河北'

(2)在功能不变的前提下,将上述三表合并成一个表,该如何设计?在你的设计中,实现上述查找的SQL语句该如何写

CREATE TABLE 国省县 (

国名:vARCHAR(50),

省市名:varchar(50),

县郡名:VarChar(50)

)

SELECT

县郡名

FROM

国省县

WHERE

国名 = '中国' AND 省市名 = '河北'


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10040487.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存