2014-09-26 281 views
0
<% 
'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

回答

2

首先,您應該在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

0
<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

希望這有幫助

0

空間字符似乎在SQL上丟失。

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

Conn.Execute(sql) 
Conn.Close()