我正在尝试将一些数据下载到我的数据库中。 我使用Microsoft Webmatrix进行简单的开发,使用SQL数据库设置和工作。
**更新** 标签和变量混合在一起 - 抱歉 - 现在我收到了这个错误:
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'ConnectDB'
/writenote.asp, line 17
请不要考虑安全性,因为它将在没有互联网访问且只有操作员访问权限的独立设备上运行。
以下是我用于输入数据库的内容。
“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" -->
我的表格:
<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>
下面是“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")
%>
老问题:
当我填写表格时,它一直说:
我不能让它在我的数据库中插入任何数据,并且它会一直出现http 500错误。
答案 0 :(得分:3)
在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")
%>
如果您在许多页面中进行数据库查询,请按照示例中的操作进行操作。把它放在'connect.asp'或config.asp':
中Set connect = Server.CreateObject("ADODB.Connection")
dbOpen = "PROVIDER=SQLOLEDB;Data Source=(local);UID=username;PWD=password;Database=HISTORICDATA "
您可以将其包含在使用此代码段进行数据库查询的任何页面中:
<!-- #include file="connect.asp" -->
我必须包含在 &lt; %%&gt; -tags之外。
在进行查询时使用以下语法:
connect.open(dbOpen)
dbQuery = "put your query here"
connect.execute(dbQuery)
connect.Close
Set Connect = Nothing
答案 1 :(得分:1)
我找到了一个有效的解决方案。非常感谢您的贡献和帮助。
在“Connect.asp”中,我连接到数据库并创建我稍后使用的记录集。
文档“notes.asp”通过来自其他地方的cookie获取所需的信息。此数据在同一地点处理,未显示,因为其中包含的信息不适用于公开显示:)
在左侧部分,我为SQL数据库创建输入。对于用户来说,大多数字段都是只读的,因为输入来自cookie和时间。 相同标记名的注释显示在textarea的右侧部分中。 此数据也可以打印(以无序方式)。
我会在下面发布我的代码,但不会发布CSS。 VB可能是无序的,但就像魅力一样。
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" -->
来自我的“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>
来自“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>