ASP connects SQL Server database implementation code

Source: Internet
Author: User
Tags dsn odbc access database

This article describes the use of ASP to connect SQL Server database instance code, the connection method has three kinds of connections through ODBC DSN, through OLE DB to establish a connection through the driver to establish a connection of three kinds, the following we look at the first.

 

Establishing a connection through driver

  code is as follows &nbs P;
<%&NBSP;
Const databasetype=1 
If databasetype=0 then& nbsp
dbpath= "/jb51/news.asp"  
sqlnowstring = "Now ()" &NBSP;
ystr=true 
nstr=false 
Suiji= "Rnd (ID)"  
else 
' If it is an SQL database, carefully modify the following database options  
DataServer = ' www111cnnet ' database server IP  
Datauser = "jb51net" Access database username  
DataBaseName = "jb51net" ' Database name  
databasepsw = ' Password ' Access database Password & nbsp
Sqlnowstring = "getdate ()"  
ystr=1 
nstr=0 
suiji= "newid ()" &NBSP;
end if 
On Error Resume next 
If databasetype = 1 then 
connstr= "Driver={sql server};server=" &dataserver& amp; "; Uid= "&datauser&"; Pwd= "&databasepsw&";D atabase= "&databasename
else 
ConnStr =" provider= microsoft.jet.oledb.4.0;Data source= "& Server.MapPath (DBPath)  
End if 
Set conn = Server.CreateObject (" ADODB. Connection ")  
Conn.Open connstr 
If Err Then Err.Clear:Set = conn" Database connection error. Please check the database parameter settings in the Conn.asp file. ": response.end 
%>

Establishing a connection through driver

It is also not necessary to create an ODBC DSN data source by driver the connection between the page and the database, but you must know the actual database file path or the data source name (for example, SQL Server database).

The code is as follows
Set conn=server.createobject ("ADODB. Connection ")
Conn. Open "Driver={sql server};server=abc;datasource= (test); uid=;p wd=;d Atabase=userdb"


Writing scripts and database sources to establish a connection

The ADO (ActiveX Data Objects) provides a Connection object that you can use to establish and manage connections between applications and ODBC databases. The Connection object has a variety of properties and methods that you can use to open and close a database connection. To write a database connection script, you should first create an instance of the Connection object, and then open the database connection

  code is as follows &nbs P;

' ****************************************************************
connection with SQL Server2000
' can reference it to establish your database connection
' **********************************************************
Please note:  
' Configure strserver,struid,strsapwd,strdbname four parameters
Dim strserver,struid,strsapwd as appropriate. strDbName
strserver= (local) ' Database server name
struid= ' testuser ' your login account
strsapwd= ' 12345 ' your login password
strdbname= Db_test_com "' Your database name

Dim Conn ' Database Connection
Dim strDSN ' Database Connection string
Dim Rs ' command string  
strdsn= ' Driver={sql serv Er};server= "&StrServer&" uid= "&StrUid&";p wd= "&StrSaPwd&";d atabase= "&strdbname
" Establish a connection to the database master
Set Conn = Server.CreateObject ("ADODB. Connection ")  
Set Rs=server.createobject (" ADODB. RecordSet ")
Conn.Open strdsn

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.