ASP connects SQL Server database implementation code

Source: Internet
Author: User
Tags odbc

Establishing a connection through driver

The code is as follows Copy Code
<%
Const databasetype=1
If Databasetype=0 Then
Dbpath= "/jb51/news.asp"
Sqlnowstring = "Now ()"
Ystr=true
Nstr=false
Suiji= "Rnd (ID)"
Else
' If it is a SQL database, please carefully modify the following database options
DataServer = "www111cnnet" ' Database server IP
Datauser = "Jb51net" ' Access to database user name
DataBaseName = "Jb51net" database name
DATABASEPSW = "Password" access to the database password
sqlnowstring = "getdate ()"
Ystr=1
Nstr=0
Suiji= "newid ()"
End If
On Error Resume Next
If DatabaseType = 1 Then
Connstr= "Driver={sql server};server=" "&dataserver&"; 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 = Nothing:Response.Write database connection error, 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 Copy Code
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 copy code

' **********
connection with SQL Server2000
' You can reference it to establish your database connection
' * * *
' Please note:
' Configure strserver,struid as appropriate, Strsapwd,strdbname four parameters
Dim strserver,struid,strsapwd,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 server};server= ' &StrServer& uid= ' &struid & ";p wd=" &StrSaPwd& ";d atabase=" &strdbname
Connection to 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.