|
Database -- Add New Record with SQL
statement
This page demonstrates the capabilities how to add a record
to a database with a SQL statement. To
double check it is in the database try:
filename=/learn/test/db1parm.asp
<TITLE>db1parm.asp</TITLE>
<body bgcolor="#FFFFFF">
<%
' My ASP program that talks to a database
set conntemp=server.createobject("adodb.connection")
conntemp.open "DSN=Student;uid=student;pwd=magic"
p1=request.querystring("ID")
temp="select * from authors where AU_ID=" & p1
set rstemp=conntemp.execute(temp)
howmanyfields=rstemp.fields.count -1
%>
<table border=1>
<tr>
<% 'Put Headings On The Table of Field Names
for i=0 to howmanyfields %>
<td><b><%=rstemp(i).name %></B></TD>
<% next %>
</tr>
<% ' Now lets grab all the records
do while not rstemp.eof %>
<tr>
<% for i = 0 to howmanyfields%>
<td valign=top><% = rstemp(i) %></td>
<% next %>
</tr>
<% rstemp.movenext
loop
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing%>
</table>
</body>
</html>
If the script doesn't work when adapting it
to YOUR database, see:
http://www.learnasp.com/freebook/asp/dbtroubleshoot.aspx
to determine what the trouble is.
The script is:
filename=/learn/test/dbnewrecSQL.asp
<TITLE>dbnewrecSQL.asp</TITLE>
<body bgcolor="#FFFFFF">
<HTML>
<%
'on error resume next
auname=request.querystring("name")
auyear=request.querystring("year")
auID=request.querystring("ID")
If auid<9000 then
auid=auid+9000
end if
Set Conn = Server.CreateObject("ADODB.Connection")
conn.open "DSN=Student;uid=student;pwd=magic"
SQLStmt = "INSERT INTO authors (AU_ID,author,year_born) "
SQLStmt = SQLStmt & "VALUES (" & auid
SQLStmt = SQLStmt & ",'" & auname & "'"
SQLStmt = SQLStmt & "," & int(auyear) & ")"
Set RS = Conn.Execute(SQLStmt)
set rs=nothing
If err.number>0 then
response.write "VBScript Errors Occured:" & "<P>"
response.write "Error Number=" & err.number & "<P>"
response.write "Error Descr.=" & err.description & "<P>"
response.write "Help Context=" & err.helpcontext & "<P>"
response.write "Help Path=" & err.helppath & "<P>"
response.write "Native Error=" & err.nativeerror & "<P>"
response.write "Source=" & err.source & "<P>"
response.write "SQLState=" & err.sqlstate & "<P>"
else
response.write "No VBScript Errors Occured" & "<P>"
end if
IF conn.errors.count> 0 then
response.write "Database Errors Occured" & "<br>"
response.write "<b>" & SQLstmt & "</b><P>"
for counter= 0 to conn.errors.count
response.write "Error #" & conn.errors(counter).number & "<P>"
response.write "Error desc. -> " & conn.errors(counter).description & "<P>"
next
else
response.write "No Database Errors Occured!" & "<P>"
end if
Conn.Close
set conn=nothing
%>
</BODY>
</HTML>
|