Lesson 6 of ASP literacy course-add new data

Source: Internet
Author: User
In the previous lesson, we reviewed what we talked about in the past and focused on the use of the IIS debugging platform. If you still have any questions, please take a look at the animation of the previous lesson, in order to ensure that the debugging station can work normally, because all future learning needs to be done under the debugging station. From this lesson, we will talk about data storage.

 

The so-called data storage is to save the data filled in by the users in the front console to the database. The purpose of storing the data to the database is to query and retrieve the data in the future, therefore, we can understand the data storage. We use asp to write programs around data, and take data storage, modification, query, and deletion as the core, for this core, we use a script that can be recognized by the database to operate on it, so as to achieve our goal. In this lesson, we need to talk a lot about the content. Let's look at the code first.

 

Let's open the previously created doing. asp and enter the following code:

The HTML code snippets attached to this Article are as follows: <%
Set conn = server. createobject ("ADODB. CONNECTION ")
Connstr = "DBQ =" + server. mappath ("db. mdb") + "; DefaultDir =; DRIVER = {Microsoft Access Driver (*. mdb )};"
Conn. open connstr

 

'Get form data
Title = Request. Form ("Title ")
Message = Request. Form ("Message ")

 

Set Rs = Server. CreateObject ("Adodb. Recordset ")
Rs. Open "Select * From Guestbook Where 1 = 2", Conn, 1, 3
Rs. Addnew
Rs ("title") = title
Rs ("message") = message
Rs. update
Rs. Close
Set Rs = Nothing

 

Response. Write ("data has been successfully written to the database <br> ")
Response. Write ("<a href = 'index. asp '> click to return </a> ")
Set conn = Nothing
%>

Input and save, open the browser, enter http: // 127.0.0.1, and leave a message to see if the prompt "data has been successfully written to the Database" appears, it means that your program is no longer faulty. Open the database and check whether the data table we designed contains the words you just made. If so, everything works normally.

 

Next we will introduce the code,
Connstr = "DBQ =" + server. mappath ("db. mdb") + "; DefaultDir =; DRIVER = {Microsoft Access Driver (*. mdb )};"
Set conn = server. createobject ("ADODB. CONNECTION ")
Conn. open connstr
This section is not detailed for the time being. You only need to know the database. mdb is our database name, and this code is used to establish a connection with the database. I will explain it in detail later.
Let me give you an example. Just as we need to call and communicate very far away, the premise is that we have a telephone and a communication telephone line. this section can be understood as an intermediary between asp and database communication.

 

'Get form data
Title = Request. Form ("Title ")
Message = Request. Form ("Message ")
We have already explained this section in detail in Lesson 4. If you do not understand it, please read the content of Lesson 4.

 

Set Rs = Server. CreateObject ("Adodb. Recordset ")
This sentence is used to create objects. You must remember that the method for creating objects in asp is to use set
Syntax:
Set object name = Server. CreateObject ("Object Name ")
Server: indicates a Server.
Create: Create
Object: indicates an Object and a target
Server. CreateObject is used together to create an object on the Server.
In the short code just now, we have seen two objects created:
Set Rs = Server. CreateObject ("Adodb. Recordset ")
Set conn = server. createobject ("ADODB. CONNECTION ")
Let's take a look at the two created objects with different names and the rest of them are the same. You may ask about the differences in Case sensitivity. asp ignores the case sensitivity.
Note: To operate the database, you must create an object. After the object is used up, remember to use Set object name = Nothing to clear it.
Here I will give an example to illustrate what is an object
"My computer runs fast"
In this sentence, "my computer" is understood as an object, while "Fast Running" is a manifestation of an object, if I remove the "my computer" object in this sentence and simply say "the operation speed is very fast", then everyone will surely be confused and have no idea what the operation speed is very fast.

 

Rs. Open "Select * From Guestbook Where 1 = 2", Conn, 1, 3
Let's take a look at this sentence. In this sentence, Rs is an object (because we just used Set Rs = Server. createObject ("Adodb. recordset ") just created it). Now the object has been created. Let's take a look at what this object will express.
Open: Indicates opening
Now we know the basic intention of an object. Its intention is to open an object. What does it want to open?
Select: Meaning select
*: It is a wildcard number, meaning all
From: Meaning from
Guestbook: the name of the table created in our database. It indicates the Message Base.
Where: what is the meaning? However, in asp programming in Computer English, we need to understand it as a condition.
Conn is the object we created
, 1, 3. We will not elaborate on it for the moment. You just need to remember that if we need to modify, add, or delete a database, 1. If we want to query the data without changing it, we need to use, 1, 1

 

Rs. Open "Select * From Guestbook Where 1 = 2", Conn, 1, 3
Well, we can understand the meaning of this sentence in a coherent manner: "to open an object (Select data from the Guestbook table in the database, the condition must meet the data of 1 = 2 ), and data can be changed"
So let's think, 1 = 2, is this possible? It is impossible. Since the condition does not exist, Select * From Guestbook (selecting all data From the database must satisfy the record with the condition of first and second ), if no row can meet the requirements, no records will be obtained,
Since no data is available, We can insert it into. The insert syntax is
Object. Addnew
Addnew: Indicates creating a database. When this statement is used, we need to know that the database is ready to create new data in, but it has not been written in. Here we need to consider a bit, that is, how to let the Data know that the title of the user in the background can be correctly written to the title column of the Message title stored in the database?
Therefore, we need to describe it to make it clear to the database.
Rs ("title") = title
Let's take a look at this Rs ("title"). We all know rs. Rs ("title") indicates the title in the object, then the object refers to our database. The meaning of this sentence is the title column in the database, and the = title at the back is the pay value. The Right Value of the equal sign is assigned to the left side of the equal sign, the right side of the equal sign is a title. This title is a variable because it is not enclosed in single quotes or double quotes, so it is a variable. If this sentence is written like this
Rs ("title") = "title"
Then the meaning is different. The meaning of this sentence becomes that the value of the title column in the database is equal to the value of the title string, so that no matter how the foreground user submits the title content of the message, the message title written to the database will always be the title character.
Now let's take a look at Rs ("title") = title again. Here we make the value of the title column of the database equal to the value of the variable title, and the value of the title is equal to the value of the Request. form ("Title"), which means to obtain from the front-end that the form project name is the value of the title (this is what we have discussed in section 4. If you do not understand it, so please look back)
Rs ("message") = message
This sentence is the same as above and will not be repeated.

 

Rs. update I already said that when we encounter rs. we need to know that the database is ready to create new data, but now we need to know that when we encounter Rs. update indicates that the program is executing the process of writing data to the database.
Rs. Addnew prepare for writing
Rs ("title") = title tells the database how to write
Rs ("message") = message tells the database how to write
Rs. update write in

 

Rs. Close closes the object. Remember that if the object is no longer in use, you must Close it in time.
Set Rs = Nothing: clears objects. clearing objects corresponds to using set to create objects,
It can be seen that the object must be created first before it can be used. After it is used up, it must be closed and cleaned up. Such a program is the most efficient program.
If you do not close or clean up, the server content we use during object creation will not be cleared for a long time, unless the server is restarted, if the program cannot be cleared, the program running efficiency is low and the speed is slow.

 

Response. Write ("data has been successfully written to the database <br>") is used to output this sentence "data has been successfully written to the Database" to the monitor, while performing line feed operations
Response. Write ("<a href = 'index. asp '> click return </a> ").
Set conn = Nothing clear object

 

Okay, this lesson is over. I will talk a lot about it. Don't remember it. The key is understanding. Programmers don't need to remember it, if you want to learn the program through rote memorization, I suggest you give up now and don't delay your time.

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.