Monday, June 9, 2008

ASP Tutorial : Code for Displaying Column Names of a Table

ASP Tutorial
How to display Field Names of a Table

This is an ASP tutorial. It serves the following purposes:
Suppose, your database contains 200 tables. How you will know what are the names of tables?
You have to open sqlplus and type the statement like this:
Select * from tab.
But If you dont have facility to access sqlplus, then you can write an asp program like this and simply enter the above statement in the form and you will get the output.
To view the list of field names, you can give the statement like:
Select * from emp where rownum=1.
This program have two security concepts. 1. The user can type only the statements, which starts with "select...". He cannot type the statement such as "delete...,update....,insert....".
2. The user has to type a password to get the output.This password value is written in the asp code itself.
Once you have typed the url: abcinfo.com/select_stt.asp, the following form is displayed. You have to enter the select statement as shown here.and press submit button
 
The output of the above form input:
 

ASP Code:

Asp file: select_stt.asp

Database: oracle


<!-- GET THE VARIABLES-->
<%
postback_msg=Request("postback_msg")
sql=Request("sql")
password=Request("password")
If password="my_password" then
permit="yes"
Else
If sql<>"" then
Response.Redirect(
"select_stt.asp?postback_msg=CHECKPASSWORD")
End if
End if
%>
<!-- END OF GET THE VARIABLES-->
<%
Set MyConn =Server.CreateObject("ADODB.Connection")
MyConn.open="
provider=MSDAORA;
data source=my_oracle_server; user id=scott;password=tiger"
%>
<HTML>
<HEAD>
<title>select statement</title>
</HEAD>
<body>
<!-- DISPLAY ALERT MESSAGES-->
<%
response.write(sql)
If postback_msg <>"" then
%>
<table width="50%" border="0" ID="Table1">
<tr>
<th>
<marquee loop="1">
<font color="red" size="3"><%=postback_msg%> </font>
</marquee>
</th>
</tr>
</table>
<%
End If
If sql <> "" and permit="yes" then
is_select=instr(ucase(sql),"SELECT")
If is_select < 1 then
response.redirect("
select_stt.asp?postback_msg=Sorry.It is not a select statement")
End If
set ObjRs1=myconn.execute(sql)
If ObjRs1.eof then
response.redirect(
"select_stt.asp?postback_msg=Sorry.No records found")
End If
myarray=ObjRs1.getrows()
End If
%>
<!-- END OF DISPLAY ALERT MESSAGES-->

<% If sql <> "" and permit="yes" then%>
<table border="1" cellspacing=0 class="ctms">
<tr>
<th class="ctms">
SNo
</th>
<% for i=0 to (ObjRs1.fields.count - 1) %>
<th >
<%=ObjRs1.fields(i).name%>
<br>
<%=ObjRs1.fields(i).type%>
&nbsp;
</th>
<%next%>
</tr>
<%for i =0 to ubound(myarray,2)%>
<tr>
<td>
<%=i+1%>
</td>
<% for j =0 to (ObjRs1.fields.count - 1)%>
<td >
&nbsp;<%=myarray(j,i)%>

</td>
<%next%>
</tr>
<%next %>
</table>
<% End If %>

<table border="0">
<tr>
<td>
<form method="post">
<table >
<tr>
<td>
Password :
<input type="password" name="password">
</td>
</tr>
<tr>
<td>
Enter the Select Statement
</td>
</tr>
<tr>
<td>
<textarea cols=60 rows=8 name="sql"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit" name="B1">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<%
Myconn.Close
Set Myconn=Nothing
%>
</body>
</HTML>

No comments:

 
Disclaimer and Copyright