<%

%>
<!--#include file="te_config.asp"-->
<%

	sub ShowForm
%>
	<!--#include file="te_header.asp"-->
	<table border=0 cellspacing=1 cellpadding=2 bgcolor = "#ffe4b5" width=100%>
		<tr>
			<td class="smallertext">
				<a href="index.asp">管理首页</a> <a href="te_admin.asp">编辑页面</a> <a href="te_listtables.asp?cid=<%=request("cid")%>"><%=arrDesc(request("cid"))%></a> 创建表格
			</td>
			<td class="smallerheader" width=130 align=right>
			<%
			if bProtected then 
				response.write session("teFullName")
				response.write "<a href=""te_logout.asp""> (退出)</a>" 
			end if
			%>
			</td>
		</tr>
	</table>
	
	<br>
	<p class="smallerheader">
		<%=sAction%><br><br><%=sErr%>
	</p>
	<form action="te_queryedit.asp?cid=<%=lConnID & sAdd%>&queryname=<%=sQueryName%>" method="post">
	<table border=0>
		<tr>
			<td class="smallerheader">疑问名称</td>
			<td><input type="text" name="txtQueryName" class="tbflat" value="<%=sQueryName%>"></td>
		</tr>
		<tr>
			<td class="smallerheader">SQL</td>
			<td>
			<textarea cols=60 rows=8 name="txtSQL" class="tbflat"><%=sSQL%></textarea>
			</td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit" name="cmdSave" value=" 保存 " class="cmdflat"></td>
		</tr>
	</table>
	</form>
	<!--#include file="te_footer.asp"-->
	<%
	end sub

	lConnID = request("cid")
	sQueryName = request("txtQueryName")
	sSQL = request("txtSQL")

	sQueryName = request("queryname")
	sNewQueryName = request("txtQueryName")

	on error resume next

	if request("add") <> "" then
		bAdd = True
		sAdd = "&add=1"
		sAction = "创建新疑问时出现问题"
	else
		bAdd = False
		sAction = "Edit Query"
		if request("cmdSave") <> "" then
			sSQL = request("txtSQL")
		else

			OpenRS arrConn(lConnID)
	
			set cmd = server.createobject("adodb.command")
			set cat = server.createobject("adox.catalog")
	
			set cat.ActiveConnection = conn
			set cmd.ActiveConnection = conn
			
			set views = cat.views
			
			set cmd = views(sQueryName).Command
			sSQL = cmd.CommandText
	
			conn.close
			set conn=nothing
			set rs=nothing
			set cmd=nothing
			set cat=nothing

		end if
	end if
	
	if request("cmdSave") <> "" then
	
		OpenRS arrConn(lConnID)

		set cmd = server.createobject("adodb.command")
		set cat = server.createobject("adox.catalog")

		set cat.ActiveConnection = conn
		set cmd.ActiveConnection = conn
		
		cmd.CommandType = adCmdText
		cmd.CommandText = sSQL

		set views = cat.views
		if bAdd then
			views.append sQueryName, cmd
		else
			views(sQueryName).Command = cmd
		end if
		
		if err <> 0 then
			sErr = "出错了 : <br>" & err.description
			bErr = True

			conn.close
			set conn=nothing
			set rs=nothing
			set cmd=nothing
			set cat=nothing

			ShowForm
		else
			conn.close
			set conn=nothing
			set rs=nothing
			
			response.redirect "te_listtables.asp?cid=" & lConnID
		end if
	
	else
		ShowForm
	end if
%>
 

