Make a connection to Sql Server using Classic ASP

2019-09-21 09:55发布

问题:

I have created a form with an action attribute. I am able to take the values from the web form and display them (response.write...used for clarification that my values are being read) using my action_page. I have also created a database and a table on MS Studio Management. The step i'm stuck on is the code that is required in order to connect the web form values and the database. Thanks. Note: This is the action page, I did not include the form. Note: I am using notepad ++ and NOT visual studio.

<%@ Language="VBscript" %>

<%
'declare the variables that will receive the values 
 Dim name 
 Dim idnum 
 Dim product
 Dim entrydate
 Dim area
 Dim qunaity

 'receive the values sent from the form and assign them to variables
  quantity=Request.Form("quantity")
  area=Request.Form("area")
  entrydate=Request.Form("date")

  stack over
 'let's now print out the received values in the browser
  Response.Write("Name: " & name & "<br>")
  Response.Write("Quantity: " & quantity & "<br>")
  Response.Write("Area: " & area & "<br>")
  Response.Write("Date: " & entrydate & "<br>")  
  %>

回答1:

Again, don't listen to those who say "start over", learning classic asp as a beginner to web dev, as it's easy to learn and will get you the basics of web dev.

Check out ConnectionStrings.com to help finding the right connection string for you. If you have the right drivers installed (which, in this case, you should), you can probably get away with something like this:

Provider=SQLNCLI11;Server=myServerAddress;Database=myDataBase;Uid=myUsername; Pwd=myPassword;

so your code may look something like this:

<%@ Language="VBscript" %>

<%
'declare the variables that will receive the values 
 Dim name 
 Dim idnum 
 Dim product
 Dim entrydate
 Dim area
 Dim qunaity

 'receive the values sent from the form and assign them to variables
  quantity=Request.Form("quantity")
  area=Request.Form("area")
  entrydate=Request.Form("date")

  stack over
 'let's now print out the received values in the browser
  Response.Write("Name: " & name & "<br>")
  Response.Write("Quantity: " & quantity & "<br>")
  Response.Write("Area: " & area & "<br>")
  Response.Write("Date: " & entrydate & "<br>")  

  '-- now, connect to the database
  dim conn : set conn = Server.CreateObject("ADODB.Connection")
  dim connString : connString = "Provider=SQLNCLI11;Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"

  conn.Open connString
  %>

now you can send and retrieve data to/from the database. Any questions, feel free to ask!