简体   繁体   English

使用经典ASP写入SQL数据库

[英]Writing to SQL database with Classic ASP

I'm trying to pull some data down into my database. 我正在尝试将一些数据提取到数据库中。 I use Microsoft Webmatrix for easy development, with SQL database setup and working. 我使用Microsoft Webmatrix通过SQL数据库设置和工作来简化开发。

** UPDATE ** Tags and variables was mixed - sorry - now i get this error: **更新**标签和变量混合在一起-对不起-现在我收到此错误:

Microsoft VBScript runtime error '800a000d'

Type mismatch: 'ConnectDB'

/writenote.asp, line 17 

Please don't look into security as this will run on a standalone unit with no internet access and only operator access. 请不要考虑安全性,因为它将在没有互联网访问权限且只能访问操作员的独立单元上运行。

Below is what i use for input to database. 以下是我用于输入数据库的内容。

"Connect.asp": “ Connect.asp”:

<% 
dim strConnect
strConnect = "Provider=SQLOLEDB;Data Source=(local);" & _
         "Database=HISTORICDATA;User ID=userid;Password=pw"
%>
<!-- METADATA TYPE="typelib"
 FILE="C:\Program Files (x86)\Common Files\System\ado\msado15.dll" -->

My form: 我的表格:

<form action="writenote.asp" method="post">
<fieldset>
<legend>New note for <% response.write(tagname) %></legend>

Time:<br />
<input type="datetime-local" name="timestamp" value="<% Response.Write Now %>" required><br />

Object:<br />
<input type="text" name="object" value="<% response.write(tagname) %>" required><br />

User:<br />
<input type="text" name="user" value="<% response.write(loginuser) %>" required><br />

Note:<br />
<textarea name="note" rows="10" cols="30" required></textarea>
<br /><br />

<input type="submit" value="Submit">
<input type="reset">
</fieldset>
</form>

Below here is the content of "writenote.asp": 以下是“ writenote.asp”的内容:

<%

dim username,object,time,note,objConn,objs,query

username = Request.Form("user")
tag      = Request.Form("object")
time     = Request.Form("timestamp")
note     = Request.Form("note")

Set objConn = ConnectDB()
query       = "INSERT INTO notes (username,object,time,note) VALUES ('"& username &"','"& object &"','"& time &"','"& note &"')"
Set objs    = objConn.Execute(query)

Response.Redirect("notes.asp")
%>

Old problem: 老问题:

When i fill in my form it keeps saying: 当我填写表格时,它会一直说:

![Http 500 error] https://cdn2.hubspot.net/hub/501326/file-2730129857-png/blog-files/01-sharepoint-2013-the-website-cannot-display-the-page-http-500-500x249.png?t=1482420346935 ![Http 500错误] https://cdn2.hubspot.net/hub/501326/file-2730129857-png/blog-files/01-sharepoint-2013-the-website-cannot-display-the-page-http- 500-500x249.png?t = 1482420346935

I can't make it insert any data in my database and it keeps coming with http 500 error. 我无法使其插入数据库中的任何数据,并且始终出现HTTP 500错误。

In writenote.asp you can do like this 在writenote.asp中,您可以这样做

    <%
    dim username,object,time,note,dbInsert,dbOpen

    Set connect = Server.CreateObject("ADODB.Connection")
    dbOpen = "PROVIDER=SQLOLEDB;Data Source=(local);UID=username;PWD=password;Database=HISTORICDATA "

    username = Request.Form("user")
    tag      = Request.Form("object")
    time     = Request.Form("timestamp")
    note     = Request.Form("note")

    connect.open(dbOpen)
    dbInsert       = "INSERT INTO notes (username,object,time,note) VALUES ('"& username &"','"& object &"','"& time &"','"& note &"')"
    connect.Execute(dbInsert)
    connect.Close
    Set Connect = Nothing

    Response.Redirect("notes.asp")
    %>

If you are doing db queries in lots of pages, Do as you have in your example. 如果要在许多页面中执行数据库查询,请按照您的示例进行操作。 Put this in a 'connect.asp' or a config.asp': 将其放在“ connect.asp”或“ config.asp”中:

Set connect = Server.CreateObject("ADODB.Connection")
        dbOpen = "PROVIDER=SQLOLEDB;Data Source=(local);UID=username;PWD=password;Database=HISTORICDATA "

You can than include it into any page you are doing db queries with this code snippet: 然后,您可以使用以下代码片段将其包含在您正在执行数据库查询的任何页面中:

<!-- #include file="connect.asp" -->

I must be included outside the <% %> -tags. 我必须包含在<%%> -tags之外。

And when doing queries use this syntax: 在执行查询时,请使用以下语法:

    connect.open(dbOpen)
    dbQuery = "put your query here" 
    connect.execute(dbQuery)    
    connect.Close
    Set Connect = Nothing

I found a solution which worked. 我找到了可行的解决方案。 Thank you very much for your contribution and help. 非常感谢您的贡献和帮助。

In "Connect.asp" i connect to database and create recordset for which i use later on. 在“ Connect.asp”中,我连接到数据库并创建记录集,以供以后使用。

The document "notes.asp" gets the info needed through a cookie coming from elsewhere. 文档“ notes.asp”通过来自其他地方的cookie获取所需的信息。 This data is handled same place and not shown as this contains info not for public display :) 此数据在同一地方处理,未显示,因为其中包含不公开显示的信息:)

In the left section i create input for the SQL database. 在左侧部分中,为SQL数据库创建输入。 Most fields are readonly for the user as the input comes from the cookie and time. 由于输入来自Cookie和时间,因此大多数字段对用户都是只读的。 The notes for the same tagname is shown in the right sections textarea. 右侧区域的文本区域中显示了相同标记名的注释。 This data can also be printed (in an unordered way). 该数据也可以打印(以无序方式)。

I'll post my code below, but not CSS. 我将在下面发布我的代码,但不发布CSS。 The VBs might be unordered but works like a charm. VB可能是无序的,但是却像一个饰物。

Connect.asp: Connect.asp:

<% 'include file
dim strConnect,objConn,objRecordSet

Set objConn = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

objConn.Open = "Provider=SQLOLEDB;Server=.\SQLEXPRESS;" & _
           "Database=HISTORICDATA;User ID=sa;Password=PW;"

%>
<!-- METADATA TYPE="typelib"
 FILE="C:\Program Files (x86)\Common Files\System\ado\msado15.dll" -->

From my "notes.asp": 从我的“ notes.asp”中:

<%@ Language="VBScript" %>
<!-- #include file="connect.asp" -->
<!-- Cookie handling -->
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="css/styles.css" type="text/css" />
        <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <![endif]-->
<script language="javascript">
<!--
function printTextArea(notetxtarea)
{
 var elementRef = document.getElementById(notetxtarea);
 var windowUrl = 'about:blank';
 var uniqueName = new Date();
 var windowName = 'Print' + uniqueName.getTime();
 var printWindow = window.open(windowUrl, windowName, 'left=50000,top=50000,width=0,height=0');

 printWindow.document.write(elementRef.value);
 printWindow.document.close();
 printWindow.focus();
 printWindow.print();
 printWindow.close();
}
// -->
</script>
        <title>Notes for <% response.write(tagname) %></title>
    </head>
    <body onload="window.resizeTo(1000,900)">

<header>
<h1>Notes for <% response.write(tagname) %></h1>
<p>For use is now:</p>

<%
response.write("Object: " & tagname)
response.write("<br />")
response.write("User: " & loginuser)
%>

</header>

<section>

<form action="writenote.asp" method="post">
    <fieldset>
        <legend>New note for <% response.write(tagname) %></legend>

<label for="tid">Time:</label>
<input type="datetime-local" id="tid" name="timestamp" value="<% Response.Write Now %>" readonly><br />

<label for="objekt">Object:</label>
<input type="text" id="objekt" name="object" value="<% response.write(tagname) %>" readonly><br />

<label for="bruger">User:</label>
<input type="text" id="bruger" name="user" value="<% response.write(loginuser) %>" readonly><br />

<p></p>Note (max. 300 chars):</p>
<textarea name="note" rows="11" cols="30" maxlength="300" required></textarea>
<br /><br />

<input type="submit" value="Submit">
<input type="reset">
        </fieldset>
</form>

</section>

<section>
<fieldset>
<legend>Notes for <% response.write(tagname) %></legend>
    <input type="button" value="Print Notes" onclick="JavaScript:printTextArea('notetxtarea');">
<textarea name="note" rows="23" cols="40" id="notetxtarea" readonly>
<%
dim strSQL,printStr
'Open the recordset object executing the SQL statement and return records
strSQL = "SELECT * FROM dbo.notes WHERE tag = '" & tagname & "' ORDER BY time DESC"

set objRecordSet = objConn.Execute (strSQL)

'first of all determine whether there are any records
If objRecordSet.EOF Then
Response.Write("No records of " & tagname)

Else

Do While NOT objRecordSet.EOF  

Response.write(objRecordSet("time") & vbCrLf)
Response.write(objRecordSet("tag") & vbCrLf)
Response.write(objRecordSet("username")& vbCrLf)
Response.write(objRecordSet("note")& vbCrLf)
Response.write(vbCrLf)
Response.write("-----------------------")
Response.write(vbCrLf)
objRecordSet.MoveNext
Loop
End If

objRecordSet.Close
Set objRecordSet=nothing
objConn.Close
Set objConn=nothing

%>
</textarea>


</fieldset>

</section>



<div class="clearer"></div>
<footer>
<p>Things in my footer</p> 
</footer>


</body>
</html>

From "writenote.asp": 来自“ writenote.asp”:

<%@ Language="VBScript" %>
<!-- #include file="connect.asp" -->
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Execute write to file</title>
    </head>
    <body>
<%
dim username,tag,time,note,query,objs

username = Request.Form("user")
tag      = Request.Form("object")
time     = Request.Form("timestamp")
note     = Request.Form("note")


objConn.Execute = "INSERT INTO dbo.notes (tag,username,time,note) VALUES ('"+ tag +"','"+ username +"','"+ time +"','"+ note +"')"


Response.Redirect("notes.asp")

objRecordSet.Close
Set objRecordSet=nothing
objConn.Close
Set objConn=nothing

%>
    </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM