Skills | data | database | database connection
1.ASP connect to an Access database
<%
Dim conn,mdbfile
Mdbfile=server.mappath ("database name. mdb")
Set Conn=server.createobject ("Adodb.connection")
Conn.Open "Driver={microsoft Access Driver (*.mdb)};uid=admin;pwd= database password; dbq=" &mdbfile
%>
2. asp and SQL database connection:
<%
Dim conn
Set Conn=server.createobject ("Adodb.connection")
Con.open "Provider=sqloledb;data source=sql server name or IP address; Uid=sa; pwd= database password; database= database name
%>
To set up a Recordset object:
Set rs=server.createobject ("Adodb.recordset")
Rs.Open SQL statement, conn,3,2
3. SQL Common Command use method:
(1) Data record filtering:
Sql= "SELECT * from data table where field name = field value order By field name"
Sql= "SELECT * from data table where field name like '% field value ' Order By field name '
Sql= "SELECT top * from data table where field name order By field name"
Sql= "SELECT * from data table where field name in (' Value 1 ', ' Value 2 ', ' Value 3 ')"
Sql= "SELECT * from data table where field name between value 1 and value 2"
(2) Update data records:
sql= "Update data table set field name = field value where Condition expression"
Sql= Update data Table set field 1= value 1, field 2= value 2 ... field n= value n Where Condition expression "
(3) Delete data records:
Sql= "Delete from data table where condition expression"
Sql= "Delete from data table" (delete all records from datasheet)
(4) Adding data records:
sql= INSERT into Data table (field 1, Field 2, Field 3 ...) valuess (value 1, value 2, value 3 ...)
Sql= INSERT INTO Target datasheet SELECT * from source datasheet (add a record from the source datasheet to the destination datasheet)
(5) Data record statistic function:
AVG (field name) to derive a table column average
Count (*| field name) statistics on the number of data rows or the number of data rows that have a value for a column
Max (field name) gets the maximum value of a table column
Min (field name) gets the smallest value of a table column
sum (field name) adds the value of the data bar
How to reference the above function:
Sql= "Select sum (field name) as Alias from data table where condition expression"
Set Rs=conn.excute (SQL)
Using RS ("Alias") to obtain the value of the unified, other functions using the same.
(5) The establishment and deletion of the data table:
CREATE Table datasheet Name (field 1 type 1 (length), Field 2 type 2 (length) ...)
Example: CREATE TABLE tab01 (name varchar, datetime default Now ())
drop table datasheet name (permanently deletes a data table)
(6) Method of Recordset object:
Rs.movenext moves the record pointer down one row from the current position
Rs.moveprevious move the record pointer up one row from the current position
Rs.movefirst move the record pointer to the first row of the datasheet
Rs.movelast move the record pointer to the last row of the datasheet
Rs.absoluteposition=n move the record pointer to the nth row of the datasheet
Rs.absolutepage=n move the record pointer to the first row of page N
Rs.pagesize=n set every page to N records
Rs.pagecount returns the total number of pages based on pagesize settings
Rs.recordcount returns the total number of records
RS.BOF returns whether the record pointer is over the first end of the datasheet, True indicates Yes, FALSE is no
Rs.eof returns whether the record pointer is over the end of the datasheet, True indicates Yes, FALSE is no
Rs.delete deletes the current record, but the record pointer does not move down
Rs.addnew add records to the end of the datasheet
Rs.update Update datasheet Record
To determine the data is a digital type
If not IsNumeric (Request (field name)) Then
Response.Write "Not Numbers"
Else
Response.Write "Number"
End If
Operations on the database are often used. Including the connection code, SQL commands and so on, and never deliberately to remember them (I am not willing to remember this stuff), so often in the use of the time to check the books, turn over. Some relatively less use of the database may not be able to find a smooth, so now they are all summed up here, to provide you with reference.
First, the database connection method:
1.Access Database Dsn-less Connection method:
Set Adocon=server.createobject ("Adodb.connection")
Adoconn. Open driver={microsoft Access Driver (*.mdb)};D bq= "& _
Server.MapPath ("Path to Database")
2.Access OLE db connection method:
Set Adocon=server.createobject ("Adodb.connection")
Adocon.open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data source=" & Server.MapPath ("Path to Database")
3.SQL Server Connection method:
Set Adocon=server.createobject ("Adodb.recordset")
ADOcon. Open "Driver={sql Server}; server= (local); uid=***; pwd=***; " & _
"Database= database name;"
4.SQL Server OLE db connection method:
Set Adocon=server.createobject ("Adodb.connection")
Adocon.open "Provider=SQLOLEDB.1;Data source=ritant4;" & _
"User id=***; password=***; " & _
"Inital catalog= database name"
5.Oracle Connection method:
Set Adocon=server.createobject ("Adodb.connection")
Adocon.open "Driver={microsoft ODBC for Oracle};server=oraclesever.world;uid=admin;pwd=pass;"
6.Oracle OLE DB Connection method:
Set Adocon=server.createobject ("Adodb.connection")
Adocon.open "Provider=oraoledb.oracle;data source=dbname;user id=admin;password=pass;"
7.dBase Connection method:
Set Adocon=server.createobject ("Adodb.connection")
Adocon.open "Driver={microsoft dbase Driver (*.dbf)};d riverid=277;dbq=------------;"
8.mySQL Connection method:
Set Adocon=server.createobject ("Adodb.connection")
Adocon.open "Driver={mysql};d atabase=yourdatabase;uid=username;pwd=yourpassword;option=16386;"
9.Visual Foxpro Connection method:
Set Adocon=server.createobject ("Adodb.connection")
Adocon.open "Driver={microsoft Visual Foxpro driver};sourcetype=dbc;sourcedb=*.dbc; Exclusive=no; "
10.MS Text Connection method:
Set Adocon=server.createobject ("Adodb.connection")
Adocon.open "Driver={microsoft text Driver (*.txt; *.csv)};d bq=-----;" &_
"Extensions=asc,csv,tab,txt; Persist Securityinfo=false; "
11.MS text OLE DB connection method:
Set Adocon=server.createobject ("Adodb.connection")
Adocon.open "Provider=microsof.jet.oledb.4.0;data Source=your_path;" &_
"Extended Properties" text; Fmt=delimited ' "
< two >. The four commonly used SQL commands:
1. Query data records (SELECT)
Syntax: Select field serial from table Where field = Content
Example: To find all the records of the author as "Cancer" from the Book table, the SQL statement reads as follows:
SELECT * FROM book where author= ' cancer '
"*" is to remove all the fields in the Book table, such as the query's field value is a number, then the "content" will not need to add single quotes,
As a date, included in Access (#), and included in SQL Server ('),
Such as:
SELECT * FROM book where id=1
SELECT * FROM book where pub_date= #2002 -1-7# (Access)
SELECT * FROM book where pub_date= ' 2002-1-7 ' (SQL Server)
Tips:
Date function to_date is not standard SQL, not all databases are applicable, so you should refer to the database syntax when using
In addition, if you are querying incoming variables, the following are:
Strau=request.form ("author")
Strsql= "SELECT * from book where author= '" &strau& ""
If the query is a number, then:
Intid=request.form ("id")
Strsql= "SELECT * from book where id=" &intid
In many databases, such as Oracle, the above statements can be written as:
Strsql= "SELECT * from book where id= '" &intID& "".
But character types must not be written in digital format, and need to be noted.
2. Add record (Insert)
Syntax: Insert into table (Field1,field2,....) Values (value1,value2,....)
Example: Add an author is a "cancer" record in Book table:
Insert into book (Bookno,author,bookname) VALUES (' CF001 ', ' cancer ', ' Cancer no component Upload program ')
Similarly, if you use a variable as follows:
Strno=request.form ("Bookno")
Strau=request.form ("author")
Strname=request.form ("BookName")
Strsql= "INSERT into book (Bookno,author,bookname) VALUES (' &strno&" ', ' "&strau&" ', ' "&strname & "')"
3. Methods of inserting data using the AddNew of a Recordset object:
Grammar:
Rs.addnew
RS ("Field1"). value=value1
RS ("Field2"). Value=value2
...
Rs.update
4. Modify data records (Update)
Syntax: Update table set field1=value1,field2=value2,... where Fieldx=valuex
Example: Update book set author= ' Babycrazy ' where bookno= ' CF001 '
If you use a variable as follows:
Strno=request.form ("Bookno")
Strau=request.form ("author")
Strsql= "Update book set author= ' &strau&" ' Where bookno= ' "&strno" "
The Update method for the 5.Recordset object:
Grammar:
RS ("Field1"). value=value1
RS ("Field2"). Value=value2
...
Rs.update
Note: When using Syntax 3 and syntax 5, be sure to note that the type of field (especially the date type) is consistent, otherwise the chances of error are very high.
Example:
Strno=request.form ("Bookno")
Strau=request.form ("author")
Set Adocon=server.createobject ("Adodb.connection")
Adocon.open "Driver={microsoft Access Driver (*.mdb)};D bq=" & _
Server.mappath= ("/cancer/cancer.mdb")
Strsql= "SELECT * from book where bookno= '" &strno& ""
Set Rs=server.createobject ("Adodb.recordset")
Rs.Open strsql,adconn,1,3
If not rs.eof then ' if you have this record
RS ("author"). Value=strau
Rs.update
End If
Rs.close
Set rs=nothing
Adocon.close
Set adocon=nothing
6. Delete a record (delete)
Syntax: Delete table where Field=value
Example: Delete Book table in which the author is a cancer record
Delete book where author= ' cancer '
(Note: If there are more than one record for the author field in the Book table cancer, all records author to Cancer will be deleted)