ASP连接到SQL Server数据库

问题描述:

<% 
'declare the variables 
Dim Recordset 
Dim sql 
dim Conn 
Dim name1,email1,phone1,company1,title1 
name1 = request.form("TxtName") 
email1 = request.form("TxtEmail") 
phone1 = request.form("TxtPhone") 
company1 = request.form("TxtCompany") 
title1 = request.form("TxtJob") 

'create an instance of the ADO connection and recordset objects 
Set Conn= Server.CreateObject("ADODB.Connection") 
Set Recordset = Server.CreateObject("ADODB.Recordset") 


'open the connection to the database 
Conn.ConnectionString = "DSN=blah;User Id=...;Password=...;Database=...." 


'Open the recordset object executing the SQL statement and return records 
Recordset.Open 
Conn.open 

sql="INSERT INTO register (Name, email, phonenumber, company, title)" 
sql=sql & "values ('"& name1 &"','"& email1 &"','"& phone1 &"','"& company1 &"','"& title1 &"')" 


Conn.Execute(sql) 
Conn.Close() 

%> 
<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 
    <title>Sample Registration Page</title> 
</head> 
<body> 
    <form name"form" action=""> 
     <table> 
      <tr> 
       <td>Full Name:</td> 
       <td> 
        <input name="TxtName" id="TxtName" type="text" /> 
       </td> 
      </tr> 
      <tr> 
       <td>Email:</td> 
       <td> 
        <input name="TxtEmail" id="TxtEmail" type="text" /> 
       </td> 
      </tr> 
      <tr> 
       <td>Phone:</td> 
       <td> 
        <input name="TxtPhone" id="TxTPhone" type="text" /> 
       </td> 
      </tr> 
      <tr> 
       <td>Company:</td> 
       <td> 
        <input name="TxtCompany" id="TxtCompany" type="text" /> 
       </td> 
      </tr> 
      <tr> 
       <td>Job Title:</td> 
       <td> 
<input name="TxtJob" id="TxtJob" type="text" /> 
       </td> 
     </table> 
    <input name="button" ID="Button1" value="submit" type="Submit" /> 
    </form> 
</body> 
</html> 

我收到一条error 500消息,当我运行这个页面时,我不知道我的错误在哪里。ASP连接到SQL Server数据库

我也做了名为blah的名为DSN连接到我的SQL Server。

我跑了ASP的一部分,它的工作原理,但数据库部分是我的问题所在。我非常感谢任何帮助,因为我相对较新。

+1

你确定它是asp.net吗?看起来像经典的asp – 2014-09-26 23:29:25

+0

Im对不起,这是正常的asp。我新来这个。对不起,我错了。 – user2127632 2014-09-26 23:46:49

+0

这不是“正常的”ASP,因为它将在2014年成为ASP.NET。这是传统的ASP,它于2000年左右退役。 – TomTom 2014-09-27 07:21:10

首先,您应该在Web服务器上激活友好的错误显示,以便确切地知道错误是什么以及错误的位置,而不是通用错误,不要说错误500。其次,在此期间,添加一些Response.write,接着是Response.Flush,以查看发生了什么以及在哪里;例如,以显示SQL字符串建设的结果:

sql = ... 
response.write sql & "<br>" 
response.flush 

其次,你试图打开一个记录,没有相关的Command对象或SQL查询字符串;你不能这样做,事实上,你不需要任何Recordset,因为你有一个Insert查询,而不是Select查询。

最后,使用带ADODB的DSN是一个坏主意。 DSN用于ODBC,并且通过在ADODB下使用ODBC,您将为数据连接添加一个旧的无用层。您应该为SQL-Server使用最新的本机OLEDB提供程序。在网络上搜索连接字符串,您将获得几个网站,其中包含有关可用提供程序及其连接字符串的完整详细信息。

+0

非常感谢。你的回应真的很有帮助。你能告诉我如何激活友好的错误显示吗?再次感谢。 – user2127632 2014-09-27 01:13:15

+0

它取决于您使用的Web服务器的版本。在互联网上搜索“错误500”或“asp错误500”。使用Visual Studio,如果需要,甚至可以在调试模式下输入页面的执行情况。 – SylvainL 2014-09-27 01:34:15

+0

对于Windows 7,您需要打开Internet信息服务(IIS)管理器。对于整个服务器或所需的Web站点(可能有多个),转到ASP属性,在“Debugging Properties”下,将所有内容设置为True(除非“Log Errors to NT Log”知道如何阅读它们)。要激活Visual Studio Debugger,请在VBScript代码中的任意位置插入“Stop”命令。你可以添加一个测试它是必要的;例如:'if(mydebug = 1)then Stop' – SylvainL 2014-09-27 06:49:14

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 
<title>Sample Registration Page</title> 
</head> 
<body> 
<form action="registration.asp" method="POST" name="form1"> 
<table> 
<tr> 
<td>Full Name:</td> 
<td><input name="TxtName" id="TxtName" type="text" /></td> 
</tr> 
<tr> 
<td>Email:</td> 
<td><input name="TxtEmail" id="TxtEmail" type="text" /></td> 
</tr> 
<tr> 
<td>Phone:</td> 
<td><input name="TxtPhone" id="TxTPhone" type="text" /></td> 
</tr> 
<tr> 
<td>Company:</td> 
<td><input name="TxtCompany" id="TxtCompany" type="text" /></td> 
</tr> 
<tr><td>Job Title:</td> 
<td><input name="TxtJob" id="TxtJob" type="text" /></td> 
</table> 
<input name="button" ID="Button1" value="submit" type="Submit" /> 
</form> 
</body> 
</html> 

<% 
If Request.ServerVariables("REQUEST_METHOD") = "POST" then 

Dim Conn, Rs, ConnString, SQL_Insert 
Dim name1,email1,phone1,company1,title1 

name1 = request.form("TxtName") 
email1 = request.form("TxtEmail") 
phone1 = request.form("TxtPhone") 
company1 = request.form("TxtCompany") 
title1 = request.form("TxtJob") 

Set Conn= Server.CreateObject("ADODB.Connection") 
ConnString = "DSN=blah;User Id=...;Password=...;Database=...." 
Conn.Open ConnString 

SQL_Insert="INSERT INTO register (Name, email, phonenumber, company, title)" & _ 
      "values ('"& name1 &"','"& email1 &"','"& phone1 &"','"& company1 &"','"& title1 &"');" 

Conn.Execute SQL_Insert 
Conn.Close 
Set Conn = Nothing 
Set SQL_Insert = Nothing 

End If 
%> 

由于您没有从数据库中检索任何数据,因此不需要RecordSet。

复制和粘贴在一个文件&名这个代码为“registration.asp”

不要忘了与实际

更换连接字符串tutotrial访问www.w3schools.com

希望这有帮助

空间字符似乎在SQL上丢失。

sql = "INSERT INTO register (...)SPACE-MISSING-HERE" 
sql = sql & "values (...)" 

Conn.Execute(sql) 
Conn.Close()