Introduction to ASP Programming (20): Modify data record of ADO component

Source: Internet
Author: User
Tags add time insert modify sql reset
ado| Programming | Data |ado to sum up: the operation of the database is simply to display records, insert records, modify records, delete records, query records.

And when it comes to displaying the record of the output format, paging program, then combined with the insertion of records, a simple news system, article system, message system, registration system is not any problem.

The following involves managing this piece, requiring modifications to the database records.

First, to change which

Modify, not Station, but to a specific change to the corresponding. It can be vividly said that the database table in the specific line to make specific changes.
So, this time the recordset has a specific one, of course this is mainly determined by the SQL statement.

For example, sql= "select * from table where id=1" means all records for the line that the ID number is extracted is 1, and it's OK to just assign the field that needs to be modified in that row to the new value and then upload the database.

The same statement sql= "SELECT * from table where id=2" believe you can understand.

But as we are in the page, is not so fixed, there are but select a connection, or enter a form value ... Jump to a special modification page, so that all the tasks are on the Modify page, it has the SQL statement should be adaptable

such as sql= "select * from table where id=" &request.queyrstring ("id")

Second, the corresponding assignment that will be modified

It's simple, as with inserting records, to match fields and values.

RS ("cn_name") = "Cnbruce"
RS ("cn_sex") = "male"

The corresponding value, of course, can also be a variable or function

Finally, upload the update database

The same as inserting the rs.updata, in fact, observed, insert new records and update records is only more rs.addnew this line of the statement.

1,showit.asp
The file is a reference established in the previous example. It is mainly the role of display, so now, for a specific record to increase the jump to modify the page hyperlink.


<%
For i = 1 to Rs. PageSize ' use for Next loop to read the record of the current page sequentially
If Rs. EOF Then
Exit for
End If
Response.Write ("<a href=change.asp?id=" & RS ("cn_id") & "> Modify </a>")
Response.Write ("article title is:" & RS ("Cn_title"))
Response.Write ("<br> article author is:" & RS ("Cn_author"))
Response.Write ("<br> article Add Time is:" & RS ("Cn_time"))
Response.Write ("<br> article content is:" & RS ("Cn_content"))
Response.Write ("Rs. MoveNext
Next
%>



Note Response.Write ("<a href=change.asp?id=" & RS ("cn_id") & "> Modify </a>")

The value of the subsequent parameter ID is dynamic, and then the chang.asp can be seen.

2,change.asp



<!--#include file= "conn.asp"-->
<%
Id=request.querystring ("id")
%>

<%if Request.Form ("submit") = "Change" Then
Whattitle=request.form ("title")
Whoauthor=request.form ("author")
Whatcontent=request.form ("content")
Id=request.form ("id")
Set rs = Server.CreateObject ("ADODB.") Recordset ")
sql = "SELECT * from Cnarticle where cn_id=" &id
Rs. Open sql,conn,3,2
RS ("Cn_title") =whattitle
RS ("Cn_author") =whoauthor
RS ("Cn_content") =whatcontent
Rs.update
Rs.close
Set rs = Nothing
Conn.close
Set conn=nothing
Response.Redirect ("showit.asp")
Response.End
%>
<%end if%>

<%
If id<> "" Then
Set rs = Server.CreateObject ("ADODB.") Recordset ")
Sql= "SELECT * from Cnarticle where cn_id=" &id
Rs. Open sql,conn,1,1
Whattitle=rs ("Cn_title")
Whoauthor=rs ("Cn_author")
Whatcontent=rs ("Cn_content")
End If
%>
<form action= "change.asp" method= "POST" >
Title:<input type= "text" name= "title" Value=<%=whattitle%>><br>
Author:<input type= "text" name= "Author" value=<%=whoauthor%>><br>
Content:<br>
<textarea name= "Content" rows= "8" cols= "><%=whatcontent%></textarea><br>"
<input type= "Submit" value= "Change" name= "Submit" >
<input type= "reset" value= "reset" >
<input name= "id" type= "hidden" value= "<%=id%>" >
</form>




Of course, all the procuratorial, security protection has not done, a lot of bugs, they have to slowly solve.

Another kind of modification update


<%if Request.Form ("submit") = "Change" Then
Whattitle=request.form ("title")
Whoauthor=request.form ("author")
Whatcontent=request.form ("content")
Id=request.form ("id")

sql = "Update cnarticle set cn_title= '" &whattitle& "', cn_author= '" &whoauthor& "', Cn_content= '" & whatcontent& "' Where cn_id=" &id
Conn. Execute (SQL)
Conn.close
Set conn=nothing
Response.Redirect ("showit.asp")
Response.End
%>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.