【问题标题】:Spot whats wrong in the code找出代码中的问题
【发布时间】:2013-04-11 13:51:50
【问题描述】:

我昨天问了this question,得到了答复。我通读了它并认为它可以工作,但是在将代码实现到我的 .asp 页面后,我得到了一个错误。

Dim strSQL
Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("DatabaseName.mdb")
Set rsLogbook = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT TableName.FieldName FROM TableName;"
rsLogbook.Open strSQL, adoCon

'' get string and put into list
Dim myList As List(Of String) = rsLogbook("FieldName").ToString().Split(New () {","}, StringSplitOptions.RemoveEmptyEntries).ToList()

'' then close all your stuff
rsLogbook.Close
Set rsLogbook = Nothing
Set adoCon = Nothing

'' Sort your list
myList.Sort();

'' now output to where ever you want.
'' in this case, have an asp.literal control on your page where you want the text and call it myLit
For each sensor in myList
myLit.Text += "<p>" + sensor + "</p>"
Next
%>

错误提示

"Microsoft VBScript 编译错误'800a0401'

预期语句结束

Dim myList As List(Of String) = rsLogbook("sensors").ToString().Split(New () {","}, StringSplitOptions.RemoveEmptyEntries).ToList() ------------^

我非常感谢任何帮助。

我在这里尝试使用此代码做的是从数据库中提取信息并使用 .asp 和 VB 脚本将其显示在网页中。我正在使用.asp页面中的Response.Write标签来实现包含数据库中信息的字段名称

更新

<%
Dim adoCon
Dim rsLogbook
Dim strSQL
Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "DRIVER={Microsoft Access Driver (*accdb, *.mdb)}; DBQ=" & Server.MapPath("featuredvehicle.accdb")
Set rsLogbook = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT featuredvehicles.make FROM featuredvehicles;"
rsLogbook.Open strSQL, adoCon
Response.Write ("<br>")
Response.Write (rsLogbook("make"))
rsLogbook.Close
Set rsLogbook = Nothing
Set adoCon = Nothing
%>

这是包含数据库和表名的实际代码

【问题讨论】:

  • 您正在使用 c# 代码并尝试在 vbscript/ASP 中重写?您需要重写解释器指向的整行 - 这样的结构以及大多数方法在 vbscript 中不可用。
  • 你能帮我用经典的 asp 而不是 asp.net 重新编写这段代码

标签: database asp-classic vbscript


【解决方案1】:

如果您只想从数据库中检索一个字段并 Response.Write 所有行,您可以使用类似这样的方法。请注意,我已删除 VB.NET 代码来创建列表。我还在 SQL 查询中添加了 WHERE 子句,以防您不想要空值(根据 VB.NET),并添加了 ORDER BY 子句来替换 List 排序。

Dim strSQL
Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("DatabaseName.mdb")
Set rsLogbook = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT TableName.FieldName FROM TableName WHERE TableName.FieldName != '' ORDER BY TableName.FieldName ASC;" 'Add order by clause so you don't need to sort the array
rsLogbook.Open strSQL, adoCon

Do Until rsLogBook.EOF
    Response.Write(rsLogbook("FieldName")) & "<br />"
    rsLogBook.MoveNext
Loop

rsLogbook.Close
Set rsLogbook = Nothing
Set adoCon = Nothing

【讨论】:

  • 我现在使用 .accdb 而不是 .mdb 文件,因为 .mdb 文件中不支持一些附加功能。有没有办法编辑此代码以使其与 .accdb 一起使用我已经尝试过adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" & Server.MapPath("DatabaseName.accdb") 但没有成功
  • 如果您的连接字符串中有 Server.MapPath("DatabaseName.accdb"),则 DatabaseName.accdb 文件必须与此脚本位于同一文件夹中。是吗?
  • 是的,它在同一个文件夹中
  • Microsoft OLE DB Provider for ODBC Drivers error '80004005' [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified /featuredvehicle.asp, line 125 这是代码中的第 3 行
  • 您的数据库是否名为 DatabaseName.mdb,或者您是否将连接字符串更改为您的数据库名称?数据库需要密码吗?如果您还在 Access 中打开了数据库,您可能无法打开它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
  • 1970-01-01
  • 2021-07-14
  • 2015-05-17
  • 1970-01-01
相关资源
最近更新 更多