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: