Error in applying database uniqueness constraints and capturing databases in ASP
Source: Internet
Author: User
The ASP pages mentioned in this article refer to the < @PAGE languege= "VBSCRIPT" except for special statements >
When writing a program that combines ASP with a database, you often encounter problems where duplicate values are not allowed in the same datasheet, such as not allowing the same user ID to appear in the user registration program. In this case, I used to read the existing value in the database and then compare it to the value entered by the user, and if so, the prompt has been registered and must be filled in again. This can be done for the intended purpose, but it is a waste of resources because of an additional database access operation.
As we all know, the columns in the datasheet can set a uniqueness constraint, that is, to restrict the field from the database to duplicate values, the database will return a 2627 error after violating the uniqueness constraint, prompting "violation of the%1! Bound '%2! '. You cannot insert a duplicate key in the object '%4! ', which is the output of the database, and all errors in SQL Server are stored in the system table master.dbo.sysmessages. User-defined messages can also be stored in sysmessages. When a stored procedure is invoked in an ASP, because the stored procedure does not have fault-tolerant processing, it returns the prompt to the page and terminates the execution of the statement, @ @Error returns a value of 0. If the page has fault tolerance, the error message does not appear, but because the error is present in the database, Err.Number cannot get its error number. (also may be my database is not familiar with the reason:-), also please shrimp pointing). Recently studied ADO, found a more convenient way to implement the control.
ADO (ActiveX Data Objects) is widely used in the application and database connection, ASP can also through him to implement the operation of the database. The connection object in ADO 2.1 has an Error object that allows you to capture errors from the database for control in the ASP page. The object's commonly used property has the following, read-only:
Description: Character type, returning error description from database
Source: Character type, error source
SQLState: Character type, ODBC API (Driver Manager) error, as described in SQL Server 2000:
The string value returned by SQLSTATE consists of a two-character class value followed by a three-character subclass value. A class value of 01 indicates a warning followed by the Sql_success_with_info return code. As long as the class value is not (except for IM), it is an error, followed by the Sql_error return code. Class IM is specifically used to indicate warnings and errors that are generated from an ODBC implementation. The neutron class value 000 of any class is used to implement the conditions defined in the given class. The allocation method for this class value and subclass value is defined by SQL-92.
Digress to digress, back to just the topic ~ ~ ~
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