Use databases in ASP

xiaoxiao2021-03-05  23

ASP has a most important feature that it allows you to connect to the database very easily. It is usually connected to an Access or a SQL database. Because Access is the easiest to start, at the same time, it may already have Access on your machine, so in the following example, we will use Access to do an example. Once you have learned the core technical method of the ASP and Access database connections, when you start using SQL Server, you will find that the key technologies needed to be substantially identical.

Transfer from: Dynamic Network Production Guide www.knowsky.com

When you want to connect to the database, you need to open this database on the server. You can connect and open the database by using a data source (DSN) or by using a method of using a DSN-LESD connection in your scripting language.

Creating a Data Source Name (DSN) You can make your database can be used in the ASP by establishing a system DSN to create a system DSN in the control panel. You can build a number of DSNs on your local computer, each DSN, corresponding to the different databases you use. After the DSN is established, you can test your page on your local server. If your website is

ISP provides services, and this ISP supports ASP, so it is possible that it will provide a GUI interface to create a DSN for your database.

In Windows 95/98 / NT, open the control panel (Start Menu -> Settings -> Control Panel), double-click ODBC to enter. Select System DSN and click Add. Select "Microsoft Access Driver" and click on the end. Fill in the data source. This is the name you give to your database, so it is the same as an alias. Click the Select button in the database selection to browse the location stored in the system you created. Click OK Now, the new DSN will now be displayed in the system DSN and can be used on your local server.

The connection database allows us to create a DSN-LESS connection and see how to connect to the database. When you create a DSN, you have stored some information about this database, so you don't need to repeat them every time you need to use some information, this information such as database type, name, storage location, optional Sex, users and passwords.

To create a DSN-LESS connection, you need the same information. The following example shows how to create a DSN-LESS connection to a database called Products:

<% Strconnect = "driver = {microsoft access driver (* .mdb)}; dbq = c: /db/products.mdb" set objconn = server.createObject ("adoDb.connection" Objconn.OpenstrConnect%>

The second line defines the driver and physical paths of the database. In order to use a DSN-LESS connection, you need to know the actual file storage location (absolute path). Server.mappath provides a simple working environment to people who use host services to find out those actual access paths that are difficult to find.

If we have established a system DSN and named Products, the connection code should be:

<% Set objconn = server.createObject ("adoDb.connection") Objconn.Open "Products"%>

Now, the database has been opened, then what can you do? The first thing is of course a series of records in the database and put them in your page. However, before this, you need a Recordset. Recordset A Recordset is all information stored on a special database table. So, when you open this RECORDSET, all the lines and columns in the table are accessible. You need to open this Recordset, just as you need to open the database connection. Their orders are similar:

Set objrec = server.createObject ("AdoDb.Recordset") Objrec.Open "Downloadable", StrConnect, 0, 1, 2

This statement creates a RecordSet (ObjRec) called Downloadable table, which is defined in the strConnect of the Products database. With RecordSet Open, we can loop to this table and can display all of its contents on the screen. Or, we can test the contents of a particular field, or write only the content we pay attention to on the screen.

Each column represents a field. So, if the database table is as follows:

Product ID SKU Name File 1 PR12345 Product a install_a.exe 2 pr12346 Product b installa_b.exe

So, we have the following fields: ProductID, SKU, Name, and File. Your table is likely to have many additional field content, which may contain many things, such as prices or product (commodities). But this schematic can give you the concept of the most basic database table. Fill in the Recordset content is very easy to use Recordset. If you want to loop to the database and print all the information to the screen, you can follow it below:

While NOT ObjRec.eof 'Says to Do This As Long As We Haven't Reached The end of the file response.writeobjrec ("ProductID") & "," SKU ") &", "Response.WriteobjRec ("Name") & "," "File") & "
" Objrec.movenext Wend; Even if you don't have so useless, you can still write the information to COMMA- In the Delimited string, and when a new line is created in the database table, recreate a new row to record the one in the table. You can write data into the HTML table using the same method. Add your Table tab by using Response.Write, you need to remember the following points:

Your HTML tag and quotation mark. If your label or content uses quotation marks, pay attention to use dual quotes: . Use & to connect to variables and HTML / content information Select the field in the Recordset to assume that our Products database also contains one The field called OS, assuming this field is a platform delimiter. Similarly, let us assume that data stored in this field can only be as follows: Windows NT, Windows 95, Windows 98, Windows, Mac, UNIX, or Linux. Below, we can confirm which field we need to print onto the screen, but to ignore which fields. Alternatively, we can choose which fields use in a format, while additional fields use other formats, such as using different colors.

Use a simple if ..., you can provide us with more database control rights. Let us first print records about Windows NT products:

Windows NT Products

<% While Not Objrec.eof

IF Objrec ("OS") = "Windows NT" Then 'Specifier The Criteria

Response.write "

"& objrec (" produsterid ") &" Response.write "" & objrec ("SKU") & " "Response.write" "& objrec (" Name ") &" "Response.write" "& objrec (" file ") &" "

End if ObjRec.movenext Wend

%> Add a record Once you start using Recordset and ASP, you will also want to add data to the database via the network. What is added is very important, for example, when you need your webview, you can leave their views and opinions, or when you want to manage updates.

The following code opens a Recordset, which is related to a database table with books and their author name. You may have seen this, but this time, the last three manual defined pointer types are different: AdopenStatic, AdlockOptimistic, Adcmdtable:

<% 'Database connection already made; code not shown hereSet objRec = Server.CreateObject ( "ADODB.Recordset") objRec.Open "books", bookdb, adOpenStatic, adLockOptimistic, adCmdTable%> (if you are not using replication adovbs.inc Document, the third line should be: Objrec.Open "Books", BookDB, 3, 3, 2).

Recordset is now ready to receive data, you just need to tell it what to add. In this case, suppose we remove the variables from the table: StrbookTitle and Strbookauthor. Our Table, Books has two fields, called Title and Author, so we can add a new record by using the following statements:

<% ObjRec.addnew Objrec ("Title") = StrbookTitle Objrec ("Author") = STRBOOKAUTHOR OBJREC.UPDATE%>

StrbookTitle and Strbookauthor represent the value, usually by users. If you just want to test the add function, you can add a variable to Title and Author - just need to remember to use quotation marks. When you use it for the first time, you may open your database immediately to make sure the update occurs.

Recordset Type In the displayed Objrec.Open example, you will find the words of 0, 1, 2 at the end. These numbers represent different pointer types. The type you use is dependent on what you will use. For example, if you don't need to modify or add any records, you can use a Lock type. And when you plan to modify or update the database, the type you choose is different.

0, 1, 2 actually represent:

AdopenForwardonly, AdlockReadonly, Adcmdtable

Of course, if you already have an Adovbs.inc's backup on your server, you can use these words directly without using numbers. Adovbs.inc includes a list of these three constants and other constants.

转载请注明原文地址:https://www.9cbs.com/read-36268.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.034, SQL: 9