技術 > asp與數據庫連接
目錄
No. 1
  <%@language="vbscript" codepage="936"%>
  <%response.buffer=true%>
  <%
  issqldatabase=0 '定義數據庫類別,0為access數據庫,1為sql數據庫
  if issqldatabase=0 then
  ''''''''''''''''''''''''' access數據庫 ''''''''''''''''''''
  datapath ="include/" '數據庫目錄的相對路徑
  datafile ="data.mdb" '數據庫的文件名
  connstr="provider=microsoft.jet.oledb.4.0;data source="&server.mappath(datapath&datafile)&""
  sqlnowstring="now()"
  sqlchar="'"
  '''''''''''''''''''''''''''''''''
  else
  '''''''''''''''' sql數據庫 ''''''''''''''''''''
  sqllocalname ="(local)" '連接ip [ 本地用 (local) 外地用ip ]
  sqlusername ="sa" '用戶名
  sqlpassword ="1" '用戶密碼
  sqldatabasename="mydbname" '數據庫名
  connstr = "provider=sqloledb;user id=" & sqlusername & "; password=" & sqlpassword & "; initial catalog = " & sqldatabasename & "; data source=" & sqllocalname & ";"
  sqlnowstring="getdate()"
  '''''''''''''''''''''''''''''''''''''
  end if
  on error resume next
  set conn=server.createobject("adodb.connection")
  conn.open connstr
  if err then
  err.clear
  set conn = nothing
  response.write "數據庫連接出錯,請檢查連接字串。"
  response.end
  end if
  on error goto 0
  %>
No. 2
  <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
  <%Response.Buffer=True%>
  <%
  IsSqlDataBase=0 '定義數據庫類別,0為Access數據庫,1為SQL數據庫
  If IsSqlDataBase=0 Then
  ''''''''''''''''''''''''' Access數據庫 ''''''''''''''''''''
  datapath ="include/" '數據庫目錄的相對路徑
  datafile ="data.mdb" '數據庫的文件名
  Connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.mapPath(datapath&datafile)&""
  SqlNowString="Now()"
  SqlChar="'"
  '''''''''''''''''''''''''''''''''
  Else
  '''''''''''''''' SQL數據庫 ''''''''''''''''''''
  SqlLocalName ="(local)" '連接IP [ 本地用 (local) 外地用IP ]
  SqlUsername ="sa" '用戶名
  SqlPassword ="1" '用戶密碼
  SqlDatabaseName="MyDBname" '數據庫名
  ConnStr = "Provider=Sqloledb;User ID=" & SqlUsername & "; Password=" & SqlPassword & "; Initial Catalog = " & SqlDatabaseName & "; Data Source=" & SqlLocalName & ";"
  SqlNowString="GetDate()"
  '''''''''''''''''''''''''''''''''''''
  End If
  On Error Resume Next
  Set conn=Server.CreateObject("ADODB.Connection")
  conn.open ConnStr
  If Err Then
  err.Clear
  Set Conn = Nothing
  Response.Write "數據庫連接出錯,請檢查連接字串。"
  Response.End
  End If
  On Error GoTo 0
  %>