Encapsulate the database connection code in a function and call this function when reading is required. The following is the SQL connection code:
Copy the code code as follows:
Function Open_conn()
dim Conn,Strconn
set Conn=server.createobject(adodb.connection)
Strconn = Provider = Sqloledb; User ID = database login account; Password = database login password; Initial Catalog = database name; Data Source = (local);
Conn.open Strconn
set Open_conn=Conn
If Err Then
err.Clear
Conn.close:set Conn=nothing
Response.Write Sorry, database connection error.
Response.End
End If
End Function
Calling method:
replace the original
Copy the code code as follows:
rs.open sql,conn
Change to
Copy the code code as follows:
rs.open sql,Open_conn()
The following is the ACCESS connection code:
Copy the code code as follows:
Function Open_conn()
dim Dbpath,Conn
Dbpath=server.MapPath (database path)
set Conn=server.createObject(ADODB.connection)
Conn.open data source=&dbpath&;provider=microsoft.Jet.OLEDB.4.0;
set Open_conn=Conn
If Err Then
err.Clear
Conn.close:set Conn=nothing
Response.Write Sorry, database connection error.
Response.End
End If
End Function
Calling method:
replace the original
Copy the code code as follows:
rs.open sql,conn
Change to
Copy the code code as follows:
rs.open sql,Open_conn()