Database Manipulation in ASP
You started learning ASP because you need to create a big, complex site, interactive and easy to update – otherwise, you can use the HTML and save all this trouble. So, if you have a lot of information, you need to store it someplace, and order it – so you'll end up putting it in a database.
ASP works well with databases, in fact, it was designed with this purpose in mind. You can modify the data in the database, and your web site will modify by itself, quickly and without messing up the rest of the application. For ASP, you can use any database you want, but, as it is Microsoft technology, it would probably be better to use Microsoft Access, which is not only fully compatible with ASP, but also easy to use, intuitive and it comes with every Microsoft Office CD. So, if you have Word and Excel on your computer, you have Access as well. Spend some time to get familiar with it. You will need to organize your data carefully, since the database will represent the "skeleton" of your entire application. If you want to, you can use MS SQL Server, Oracle, MySQL Databases or any other type of database you are familiar with, it won't make a big difference.
Then, you need to connect the database, and there are two methods to do with: with or without DNS (Data Source Name). If your site is hosted by a company (as is usually happens), you have to contact them and ask them to set up the DNS for you (you won't take them by surprise with this request, I'm sure they've heard of it before). You will have to tell them where your database is located, and the name chosen for your DNS.
In order to make the interaction with the database easier, Microsoft has created a group of objects – also known as the ADO technology (ActiveX Data Objects). There is a Connection Object, for the connection with your database, and a Recordset Object, for getting the data out of the database. These objects act just like the one we've seen before, and they have properties and methods too. For instance, the Connection object has methods such as Open, Close, Execute, and properties such as ConnectionTimeOut, State, Provider, Version. As you can see, they are in plain English, it's quite easy to understand what each of them does.
Besides using the RecordSet object, another method of adding records to the database is by using SQL statements. SQL stands for Structured Query Language and it represents the standard language that deals with databases. It is also quite intuitive, for instance, the statement you will be using in order to add a record is "insert". You will also find statements such as "select", "delete" and "update". With the "insert" statement, you will need to tell it the name of the database, the fields, or columns where you want the data inserted, and the actual data you want to insert, like this:
insert into database_name (column01, column02) values ("value01", "value02")
A database can store anything. You can ad pictures, HTML forms, anything else you see on the Internet today. It is important to keep them well organized, do not mix data that don't belong together, or are not of the same time, and pay close attention to the relations among them. The best design you can create for your page will not help the users, if they have problems accessing the content.
Once you start using databases, you actually use the ASP for the purpose it was designed. As your scripts get bigger and more complex, they are also a bit more difficult to handle. You can write a code separately, and then include it where you need it. For this, you have the #include directive, used for elements that you want to keep separately, for better organization, or that you want to re-use on several pages. The syntax is:
<!–#include file ="file_name"–>
Other posts in asp
- What is ASP?
- Installing ASP
- First ASP Script
- JScript Syntax
- JScript Data Types
- Variables in JScript
- Operators & Expressions in ASP
- Arrays in JScript
- Conditional Structure in JScript
- Loop Structures in JScript
- Functions in JScript
- Object Oriented Programming in ASP
- Classes in ASP
- Response and Request in ASP
- File Manipulation in ASP
- Cookies in ASP
- Sessions in ASP
- Debugging & Efficiency in ASP
- Beyond ASP