比如数据库是AAmdb,数据表是省市表,有字段省、市、县等等,那么:
Private Sub Form_Load()
'连接数据库语句略
strSQL = "SELECT Distinct 省 FROM 省市表"
RSOpen strSQL, db, 2, 2
Do While Not RSEOF
Combo1AddItem RS!省 '这里给Combo1加载省名称
RSMoveNext
Loop
RSClose
Set RS = Nothing
'关闭数据库连接对象
End Sub
Private Sub Combo1_Click()
Combo2Clear
'连接数据库语句略
strSQL = "SELECT Distinct 省,市 FROM 省市表 Where 省='" & Combo1Text & "'"
RSOpen strSQL, db, 2, 2
Do While Not RSEOF
Combo2AddItem RS!市 '这里给Combo2加载市名称
RSMoveNext
Loop
RSClose
Set RS = Nothing
'关闭数据库连接对象
End Sub
Combo3与上面同理,在Private Sub Combo2_Click() 加载县名称,不再说了。
不出本市行程卡是没有记录的。行程卡是通过国家大数据中心数据库提取的数据,行程的最小单位就是地市级,不会识别到某市的某个县或某个区。当然也是有些误差的,当你来到两个市的交界处市,虽然您没有离开本市,但也有很大可能显示你到过其他市。
省、市联动
private DataSet dsProvince = null;//省
private DataSet dsCity = null;//市
private DataView dvCity = null;
public string provinceId = "";//省级编号
public string cityId = "";//市级编号
private string idStr = "";
private string province;//省级名称
private string city;
private void CityControl_Load(object sender, EventArgs e)
{
lkpProvinceVisible = ProvinceShow;
lkpCityVisible = CityShow;
lkpCountyVisible = CountyShow;
lkpProvinceText = Province;
provinceId = SetBh(1, "0", province);
lkpCityText = City;
cityId = SetBh(2, provinceId, city);
}
/根据编号和父编号查询符合条件的一行,并返回这行的名字
public DataRow SetDataRow(DataTable dt, string pid, string id, out string name)
{
name = "";
DataView dvSet = dtDefaultView;
dvSetRowFilter = " CITYID='" + id + "' and PARENTID='" + pid + "' ";
DataTable dataTable = dvSetToTable();
if (dataTable != null && dataTableRowsCount > 0)
{
name = dataTableRows[0]["CITY"] == null "" : dataTableRows[0]["CITY"]ToString();
return dataTableRows[0];
}
return null;
}
//根据名字和父编号,返回本身编号
private string SetBh(int index, string pid, string name)
{
DataTable dt = null;
if (index == 1)
{
if (dsProvince != null && dsProvinceTables != null && dsProvinceTables[0] != null && dsProvinceTables[0]RowsCount > 0)
{
dt = dsProvinceTables[0];
}
}
else if (index == 2)
{
if (dsCity != null && dsCityTables != null && dsCityTables[0] != null && dsCityTables[0]RowsCount > 0)
{
dt = dsCityTables[0];
}
}
string bh = "";
if (dt != null && dtRowsCount > 0)
{
if (pid != "" && name != "")
{
DataView dvSet = dtDefaultView;
dvSetRowFilter = " CITY='" + name + "' and PARENTID='" + pid + "' ";
DataTable dataTable = dvSetToTable();
if (dataTable != null && dataTableRowsCount > 0)
{
bh = dataTableRows[0]["CITYID"] == null "" : dataTableRows[0]["CITYID"]ToString();
}
}
}
return bh;
}
#region 省级
private void lkpProvince_Leave(object sender, EventArgs e)
{
province = thislkpProvinceText + "";
#region 第一步:确定省级状况
if (province == "")//当未选择任何省份时
{
provinceId = "";
province = "";
cityId = "";
city = "";
countyId = "";
county = "";
lkpProvinceText = "";
lkpCityText = "";
lkpCountyText = "";
SetDorp(0, "", false, out idStr);
return;
}
//判断当前省份在列表中是否存在
DataRow[] prRow = dsProvinceTables[0]Select(" CITY='" + province + "'");
if (prRow != null && prRowLength > 0)
{
province = prRow[0]["CITY"] + "";
if ((provinceId != "" && provinceId != prRow[0]["CITYID"] + "") || provinceId == "")
{
provinceId = prRow[0]["CITYID"] + "";
}
//当省文本框不为空,省ID不为空且下拉列表未选中任何行时
if (province != "" && provinceId != "" && (lkpProvinceSelectedRow == null || lkpProvinceSelectedRow["CITYID"]ToString() == ""))
{
thislkpProvinceSelectedRow = SetDataRow(dsProvinceTables[0], "0", provinceId, out province);
}
}
else
{
provinceId = "";
lkpCityTable = dsCityTables[0];
}
#endregion
#region 第二步:确定市级情况
if (stringIsNullOrEmpty(provinceId) && (!stringIsNullOrEmpty(cityId)))
{
cityId = "";
lkpCityText = "";
}
else if (!stringIsNullOrEmpty(provinceId))
{
lkpCityTable = SetDorp(1, " PARENTID='" + provinceId + "'", true, out idStr);
if (!stringIsNullOrEmpty(cityId))
{
if (!(("," + idStr + ",")Contains(",'" + cityId + "',")))
{
cityId = "";
lkpCityText = "";
}
else
{
thislkpCitySelectedRow = SetDataRow(dsCityTables[0], provinceId, cityId, out city);
}
}
}
#endregion
if (province != "")
lkpCityFocus();
}
#endregion
#region 市级
private void lkpCity_Leave(object sender, EventArgs e)
{
#region 第一步:确定市级情况
city = thislkpCityText + "";
if (province != "" && city == "")//当市级选择为空字符串时
{
cityId = "";
countyId = "";
county = "";
return;
}
else if (province == "" && city == "")
{
province = "";
provinceId = "";
cityId = "";
countyId = "";
county = "";
SetDorp(0, "", false, out idStr);
return;
}
//判断当前选择市是否存在于列表
DataRow[] crRow = null;
if (!stringIsNullOrEmpty(provinceId))
{
crRow = dsCityTables[0]Select(" CITY='" + city + "' and PARENTID='" + provinceId + "' ");
}
else
{
crRow = dsCityTables[0]Select(" CITY='" + city + "'");
}
if (crRow != null && crRowLength > 0)
{
city = crRow[0]["CITY"] + "";
if ((cityId != "" && cityId != crRow[0]["CITYID"] + "") || cityId == "")
{
cityId = crRow[0]["CITYID"] + "";
provinceId = crRow[0]["PARENTID"] + "";
}
//当省级ID不为空,市级ID不为空且市级列表未选择任何行时
if (province != "" && provinceId != "" && city != "" && cityId != "" && (lkpCitySelectedRow == null || lkpCitySelectedRow["CITYID"]ToString() == ""))
{
thislkpCitySelectedRow = SetDataRow(dsCityTables[0], provinceId, cityId, out city);
}
}
else
{
cityId = "";
}
if (!stringIsNullOrEmpty(provinceId))
{
thislkpCityTable = SetDorp(1, " PARENTID='" + provinceId + "'", false, out idStr);
if (!stringIsNullOrEmpty(cityId))
{
thislkpCitySelectedRow = SetDataRow(dsCityTables[0], provinceId, cityId, out city);
thislkpCityText = city;
}
}
#endregion
#region 第二步:确定省级情况
if ((!stringIsNullOrEmpty(provinceId)))
{
thislkpProvinceSelectedRow = SetDataRow(dsProvinceTables[0], "0", provinceId, out province);
thislkpProvinceText = province;
}
#endregion
}
#endregion
给你一些思路:
1、省市县的数据写到数据库中。每条数据会有一个父ID(上级ID)。
2、在页面上省变动后,查询市的数据以省的ID作为条件,县也一样,以市 的ID为条件。
第二种方法,
由于全国的这些基本上是固定的,一般变动比较少,这可以把这个数据写到JS里面,通过JS来实现。这个加载会快一些。这些数据可以到国家统计局的网站去下载。
<%
'自动获得IP
Dim uip,cip,ip
uip = RequestServerVariables("")'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dim conn,connstr,db
db="ipmdb"
Set conn = ServerCreateObject("ADODBConnection")
connstr="Provider=MicrosoftJetOLEDB40;Data Source=" & ServerMapPath(db)
connOpen connstr
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
cip=split(uip,"")
if ubound(cip)<3 then '如果提交的IP数据不足4位,就补齐
redim Preserve cip(3) '重新定义数组,并保留原来的值
for i=3 to ubound(cip) step -1
cip(i)=0
Next
End If
for i=0 to 3
if not IsNumeric(cip(i)) then cip(i)=0
Next
ip=256256256cip(0)+256256cip(1)+256cip(2)+cip(3)-1
dim rs,sql,Country,City
Set rs=ServerCreateObject("ADODBRecordset")
'自己改下相应字段名等
sql="select from Address where startip<="&ip&" and endip>="&ip
rsopen sql,conn,1,1
if rseof Then
City="未知"
Country="未知"
Else
City=rs("ip_city")
Country=rs("ip_county")
end If
responsewrite Errdescription
rsclose
set rs=Nothing
responsewrite "你要查询的IP是"&uip&",来自"&city&"-"&country
%>
数据表:
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(ConnStringConStr);
SqlCommand cmd = new SqlCommand("CateTable_GetList", con);
cmdCommandType = CommandTypeStoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
sdaFill(ds);
}
catch
{
}
finally
{
cmd = null;
conClose();
}
//往TreeView中添加树节点
//添加根节点
TreeNode tn = new TreeNode();
tnText = "种类";
tnName = "0";//Name作为ID
tnTag = "0";//Tag作为RootID
tnImageIndex = 0;
tnSelectedImageIndex = 0;
tvNodesAdd(tn);//该TreeView命名为tv
tvSelectedNode = tvTopNode;
//把其他节点加上去
if (ds != null)
{
foreach (DataRow dr in dsTables[0]Rows)
{
tn = new TreeNode();
tnText = dr["CateName"]ToString();
tnName = dr["CateID"]ToString();//Name作为CateID
tnTag = dr["RootID"]ToString();//Tag作为RootID
tnImageIndex = 1;
tnSelectedImageIndex = 1;
//判断是否为主节点
if (dr["CateID"]ToString() == dr["RootID"]ToString())
{
//主节点
tvSelectedNode = tvTopNode;
}
else
{
//其他节点
if (tvSelectedNodeName != dr["ParentID"]ToString())
{
TreeNode[] tn_temp = tvNodesFind(dr["ParentID"]ToString(), true);
if (tn_tempLength > 0)
{
tvSelectedNode = tn_temp[0];
}
}
}
tvSelectedNodeNodesAdd(tn);
}
tvExpandAll();//展开TreeView
tvSelectedNode = tvTopNode;
}
}
以上就是关于vb中combobox1中选择省,combobox2中对应出现市,combobox3出现县,combobox4……以此类推。全部的内容,包括:vb中combobox1中选择省,combobox2中对应出现市,combobox3出现县,combobox4……以此类推。、国家大数据不出市,去过别的县城能查出来吗、有人能帮忙写个二级联动吗要从数据库调得那个。就来个大概。然后能给我讲懂了就行!感谢!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)