【问题标题】:Variables losing their value变量失去价值
【发布时间】:2013-06-27 23:50:01
【问题描述】:

我有以下 vbscript 代码:

hemisphereConnectionString = "Driver={MySQL ODBC 5.1 Driver};" & _
                             "Server=" & hemisphereServer & ";" & _
                             "Database=" & hemisphereDatabase & ";" & _
                             "User=" & hemisphereUsername & ";" & _
                             "Password=" & hemispherePassword & ";"

Set hemisphereConnection = CreateObject("adodb.connection")
hemisphereConnection.Open(hemisphereConnectionString)

hem_sql = "SELECT * FROM hei_vision_update WHERE id IN (SELECT MAX(id) FROM hei_vision_update WHERE done = 'N' GROUP BY name)"

Set hemisphereResult = hemisphereConnection.Execute(hem_sql)

if hemisphereResult.EOF = false then
    hemisphereResult.MoveFirst()
end if
msgbox isnull(hemisphereResult("home_publish").value)
msgbox hemisphereResult("home_publish").value

它是一个更大的脚本的一部分(太大,无法在此处发布,但这些是关键行) 我的消息框显示为 false(即该字段的值不为空)并且下一个消息框崩溃说“无效使用 Null”

有人有什么想法吗??

我已将我的代码修改为上述内容,仅此而已(服务器和密码详细信息除外)。现在没有 OERN/OEG0 线路。我从数据库中获取的行在 home_publish 字段中有一个“Y”,因此第一个显示 false 的消息框是正确的。只有第二个显示“无效使用 Null”是个谜。我开始怀疑 MySQL 驱动程序是否存在问题?

现在越来越傻了:

我把最后两行改成了这三行:

msgbox hemisphereResult("home_publish").value
msgbox isnull(hemisphereResult("home_publish").value)
msgbox hemisphereResult("home_publish").value

第一个消息框显示我的值“Y”。
现在第二个消息框显示为真(显然不是)。
最后,我的第三个消息框给出了“无效使用 null”错误。

还有其他人经历过仅仅因为你使用过变量而失去价值的经历吗?

【问题讨论】:

  • 您选择 home_phone,但您尝试显示字段 home_publish;摆脱你的全球 OERN。
  • 抱歉,我把所有其他字段都从我的选择中删除了,因为发布时间会很长,但是 home_publish 在那里(还有 home_phone!) - 什么是全球 OERN?
  • 好的,所以 OERN 出现错误继续下一步。虽然没有帮助。如果我有它就跳过错误
  • 试试MsgBox TypeName(hemisphereResult("home_publish").Value)
  • 我添加了两次: msgbox TypeName(hemisphereResult("home_publish").Value) msg​​box TypeName(hemisphereResult("home_publish").Value) 第一个消息框说“String”,第二个说空值。不知何故,我的领域在第一次使用时就被破坏了!

标签: mysql database vbscript adodb mysql-odbc-connector


【解决方案1】:

如果hemisphereResultADODB.Recordset,那么hemisphereResult("home_publish")ADODB.Field,此时会发生以下情况。

IsNull 不会尝试进一步调查传递的对象并返回False,因为该字段本身作为记录集中的对象存在。

相反,MsgBox 不能对对象做有意义的事情,所以它调用该对象的默认属性(Field 对象)来显示它。默认属性是.Value那个Null

因此,尽管 IsNullMsgBox 的参数在代码中看起来相同,但它们实际上并不相同。

您可能希望明确提出您的要求:

msgbox isnull(hemisphereResult("home_publish").value)

虽然上述情况属实,但您也可能受到 MySQL 错误的影响(4483142385 等)

建议的解决方法是使用客户端光标:

set hemisphereResult = CreateObject("ADODB.Recordset")
hemisphereResult.CursorLocation = 3 'adUseClient
hemisphereResult.open "select ...", hemisphereConnection

【讨论】:

  • 感谢 GSerg 的回复 我将代码切换为: msgbox isnull(hemisphereResult("home_publish").value) msg​​box hemisphereResult("home_publish").value 但仍然得到完全相同的结果。此外,从数据库中,该字段的值为“Y”
  • @user2530030 修改后的IsNull还是给False吗?它应该给出True,但MsgBox 确实应该失败,因为它不会显示Null
  • 肯定还是给假!它应该给出 false,因为数据库中的字段确实有一个值。但仍然在第二个 msgbox 上崩溃说“无效使用 null”
  • @AngusWalker 您是否有 VB6 或 MS Office,您可以在其中粘贴该代码并在监视窗口中查看 hemisphereResult 值在运行时如何变化?
  • 将其加载到 Access 中并在即时窗口中处理字段时对其进行查询。只是证实了我们已经知道的。一旦字段被查询,它们就会消失!
猜你喜欢
  • 1970-01-01
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
相关资源
最近更新 更多