【问题标题】:trouble running query on Access DB in asp classic在 asp 经典中在 Access DB 上运行查询时遇到问题
【发布时间】:2014-11-07 04:54:37
【问题描述】:

我正在尝试对我的数据库运行查询,但在执行时不断收到错误 80004005 但不知道为什么?

Set adconn = Server.CreateObject("ADODB.Connection") 
adconn.Open "justlistit_changes"

Set sqlgetad1 = Server.CreateObject("ADODB.Command")
sqlgetad1.ActiveConnection = adconn
sqlgetad1.commandtext = "Select * from ads where county = '" & selcounty & "' and where position='1'"
sqlgetad1.Prepared = true
sqlgetad1.execute
adconn.Close

当我只使用county 参数时,查询工作正常,但一旦我尝试在查询中的任何位置使用position,即使单独使用,查询也不会工作。我已经尝试过硬编码和使用变量,但没有任何效果,请帮忙。

【问题讨论】:

  • 位置是什么类型的字段?试试position=1 而不是position='1'
  • 所以您使用ADODB.Command 对象但不费心为您的查询参数化?这样做有什么意义?
  • 你有完整的错误细节吗?
  • 我在使用参数时无法运行其他查询,因此我将它们全部废弃。这个网站不是我建的,我只是接手了,之前没做过经典的asp
  • @adgoodso23 那就是你的问题。 ADODB.Command 是正确的做法,你的前任是在正确的轨道上。至于"Select * from ads where county = '" & selcounty,这是记录在案的不良做法,让您对 SQL 注入持开放态度。

标签: sql vbscript asp-classic


【解决方案1】:

更新

原因是 position 是 Microsoft Access 中的 ANSI 92 保留字 - 请参阅 ACC2002: Error with ANSI-92 Reserved Words When SQL Server Compatibility Syntax (ANSI 92) Is Enabled

要解决此问题,只需将您的字段封装在方括号中 [] (更新了我下面的示例)

[位置] = ?

更新

看起来您报告的错误与您的代码无关。


您使用ADODB.Command 但在CommandText 中设置变量会绕过使用ADODB.Command 的保护,从而在执行参数化查询时为您提供。

试试这个;

Dim sqlgetad1, rs

Set sqlgetad1 = Server.CreateObject("ADODB.Command")
With sqlgetad1
  'Assuming justlistit_changes is your connection string variable.
  .ActiveConnection = justlistit_changes
  .CommandType = adCmdText
  '******** Avoid any clashes with ANSI 92 reserved words ********
  .CommandText = "Select * from ads where [county] = ? and [position] = ?"
  .Prepared = true
  .Parameters.Append(.CreateParameter("@county", adVarChar, adParamInput, 100))
  'Assuming your position field is actually a numeric value.
  .Parameters.Append(.CreateParameter("@position", adInteger, adParamInput, 4))
  .Parameters("@county").Value = selcounty
  .Parameters("@position").Value = 1
  Set rs = .Execute()
End With

如果您的 position 字段不是数字替换为

.Parameters.Append(.CreateParameter("@position", adVarChar, adParamInput, 10))
.Parameters("@position").Value = "1"

如果您对 adCmdText 等 ADO 命名常量有疑问,请查看 how to use METADATA to import DLL constants

【讨论】:

  • 我试过这个,我仍然收到错误'80004005' /wwwroot/display.asp,第 154 行。第 154 行是执行命令
  • 它与数据库中的位置字段有关。我创建了一个名为“adpos”的新字段,它工作正常。我想我会使用新字段。
  • @adgoodso23 怎么会工作,adconn.Open "justlistit_changes" 不是有效的连接字符串,您实际使用的是什么代码?
  • @adgoodso23 废弃 POSITION 是一个 ANSI 92 保留字,这将成为问题。见Update
【解决方案2】:

松开第二个“where”:

"Select * from ads where county = '" & selcounty & "' and where position='1'"

==>

"Select * from ads where county = '" & selcounty & "' and position='1'"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    相关资源
    最近更新 更多