1. Create a database name in SQL server: house runs the following script in house
If exists (select * from sysobjects where id = object_id (n' [dbo]. [City] ') and OBJECTPROPERTY (id, n'isusertable') = 1)
Drop table [dbo]. [City]
GO
If exists (select * from sysobjects where id = object_id (n' [dbo]. [Area] ') and OBJECTPROPERTY (id, n'isusertable') = 1)
Drop table [dbo]. [Area]
GO
Create table [dbo]. [City] (
[Id] [int] IDENTITY (1, 1) not null,
[Name] [nvarchar] (10) NULL
) ON [PRIMARY]
GO
Create table [dbo]. [Area] (
[Id] [int] IDENTITY (1, 1) not null,
[CityId] [int] NULL,
[Name] [nvarchar] (20) NULL
) ON [PRIMARY]
GO
Insert into City (id, name) Values (1, "Guangzhou ")
Insert into City (id, name) Values (2, "Shanghai ")
Insert into City (id, name) Values (3, "Beijing ")
Insert into Area (id, CityId, name) Values (1, 1, "Tianhe ")
Insert into Area (id, CityId, name) Values (2, 1, "")
Insert into Area (id, CityId, name) Values (3, 2, "Hongkou ")
Insert into Area (id, CityId, name) Values (4,2, "Baoshan ")
Insert into Area (id, CityId, name) Values (5, 2, "Jingan ")
Insert into Area (id, CityId, name) Values (6, 3, "Haidian ")
Insert into Area (id, CityId, name) Values (7,3, "Chaoyang ")
2. Create a file
Create the CityData. asp file
<% @ LANGUAGE = VBSCRIPT %>
<% RSDispatch %>
<Script runat = SERVER Language = javascript>
<! -- # Include virtual = "/_ ScriptLibrary/RS. ASP" -->
<! -- # Include virtual = "/jsconn. asp" -->
Function Description ()
{
This. GetAreaAsArray = DoGetData;
}
Public_description = new Description ();
Function DoGetData (cityId)
{
Var SQL, rst, strText, conn, I, j
Conn = new ActiveXObject ("ADODB. Connection ");
Conn. ConnectionString = connstr;
Conn. ConnectionTimeout = 30;
Conn. Open ();
SQL = "Select name, id from area where cityid = '" + cityId + "'";
Rst = new ActiveXObject ("ADODB. Recordset ");
Rst. CursorLocation = 3;
Rst. Open (SQL, conn );
I = 0;
J = 0;
StrText = new Array ();
If (rst. RecordCount> = 1)
{
For (var I = 0; I <rst. RecordCount; ++ I)
{
StrText [j ++] = rst. Fields ("name"). Value;
StrText [j ++] = rst. Fields ("id"). Value;
Rst. movenext ();
}
}
Else
{
StrText [j ++] = "no ";
StrText [j ++] = 1;
}
Rst. close ();
Conn. close ();
Return strText;
}
</SCRIPT>
Create File jsconn. asp
<Script runat = SERVER Language = javascript>
Var connstr = "driver = {SQL Server}; server = 202.0.0.108; uid = sa; pwd =; database = house ";
</SCRIPT>
Create a file pub. asp
<%
Function OpenOrGet_Database
Dim SessionName, conn
Const connstr = "driver = {SQL Server}; server = 202.0.0.108; uid = sa; pwd =; database = house"
SessionName = "House"
If Not IsObject (Session (SessionName) Then
Set conn = Server. CreateObject ("ADODB. Connection ")
Conn. Open connstr
Set Session (SessionName) = conn
End If
Set OpenOrGet_Database = Session (SessionName)
End Function
%>
Create the file SQL _pub.asp
<%
'Retrieve City Data
Function SelectCity ()
Dim Conn, SQL, Rs, ArrCity, TmpArr (1, 0)
Set Conn = OpenOrGet_Database
Set Rs = Server. CreateObject ("ADODB. Recordset ")
SQL = "Select distinct name, id from City"
Rs. Open SQL, Conn, 3
If Rs. Eof then
TmpArr (0, 0) = "city"
TmpArr (1, 0) = 0
Rs. Close
SelectCity = TmpArr
Else
ArrCity = RS. GetRows ()
Rs. Close
SelectCity = ArrCity
End if
End Function
'Retrieve the corresponding region information based on the CityId
Function SelectArea (CityId)
Dim Conn, SQL, Rs, ArrArea, TmpArr (1, 0)
Set Conn = OpenOrGet_Database
Set Rs = Server. CreateObject ("ADODB. Recordset ")
SQL = "Select name, id from Area where CityId =" & CityId & "order by id"
Rs. Open SQL, Conn, 3
If Rs. Eof then
TmpArr (0, 0) = "region"
TmpArr (1, 0) = 0
Rs. Close
SelectArea = TmpArr
Else
ArrArea = RS. GetRows ()
Rs. Close
SelectArea = ArrArea
End if
End Function
%>
Create File test. asp
<% @ LANGUAGE = "VBSCRIPT" %>
<% Option explicit %>
<! -- # Include VIRTUAL = "pub. asp" -->
<! -- # Include VIRTUAL = "SQL _pub.asp" -->
<%
Dim arrCity, arrArea, strPathInfo, strServerName, strServerPort, I, TmpStr
StrPathInfo = Request. ServerVariables ("PATH_INFO ")
StrServerName = Request. ServerVariables ("SERVER_NAME ")
StrServerPort = Request. ServerVariables ("SERVER_PORT ")
If len (strServerPort) = 0 then
StrServerPort = ""
Else
StrServerPort = ":" + strServerPort
End if
StrPathInfo = "http: //" & strServerName & strServerPort &""
ArrCity = SelectCity ()
ArrArea = SelectArea (arrCity (1, 0 ))
%>
<Html>
<Head>
<Title> Untitled Document </title>
<Meta http-equiv = "directory type" content = "text/html; character set = gb2312">
</Head>
<Script language = "JavaScript" src = "<% = strPathInfo %>/_ ScriptLibrary/rs.htm"> </script>
<Script language = "JavaScript">
RSEnableRemoteScripting ("<% = strPathInfo %>/_ ScriptLibrary ");
</Script>
<Body bgcolor = "# FFFFFF">
<Form method = "post" name = "select">
<Font color = "#222200" type = "codeph" text = "/codeph">
<Select name = "City" size = "1" onChange = "JavaScript: Choosesection (this. form. Area, this. form. City)">
<Option value = 0 selected> -- select a city -- </option>
<% For I = LBound (arrCity, 2) To UBound (arrCity, 2)
TmpStr = "<option value =" & arrCity (1, I )&""
TmpStr = TmpStr & ">" & arrCity (0, I) & "</option>"
Response. Write (TmpStr)
Next
%>
</Select>
</Font> <font color = "#1, 222200">
<Select name = "Area" size = 1>
<Option value = 0> -- select the town area -- </option>
<% For I = LBound (arrArea, 2) To UBound (arrArea, 2)
If arrArea (1, I) <> 0 then
%>
<Option value = <% = arrArea (1, I) %> <% = arrArea (0, I) %> </option>
<% End if
Next %>
</Select>
</Font>
</Form>
<Script language = "JavaScript">
Var serverURL = "<% = strPathInfo %> ";
Var pageURL = "/CityData. asp ";
Function Choosesection (oArea, oCity)
{
For (var I = oArea. options. length-1; I> 0; -- I)
{OArea. options. remove (I )}
Var CityId = oCity. options (oCity. selectedIndex). value;
Var objResult = RSExecute (serverURL + pageURL, "GetAreaAsArray", CityId );
Var j = 0
If (objResult. return_value.length> 0)
{
For (var I = 0; I <objResult. return_value.length/2; ++ I)
{
SName = objResult. return_value [j ++];
SId = objResult. return_value [j ++];
Var oOption = document. createElement ('option ');
OOption. text = sName;
OOption. value = sId;
OArea. options. add (oOption );
}
}
}
</Script>
</Body>
</Html>