条件表达式中的数据类型冲突
Data type conflict in criteria expression
我目前正在尝试在我的 vbscript 中使用数据库 (.accdb-file)。
我的函数来自 .vbs-file,由 hta-file:
执行
function dbCall(sAction, sPayload, sTable, sConCol, sConVal)
updateLocalDB()
Dim sConnectionString, objConnection, objRecordset, dbQuery, lTemp
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & localDB
'Query für die DB zusammenbauen
Select Case sAction
Case "get"
If sConCol = False Then
dbQuery = "SELECT " & sPayload & " FROM " & sTable
Else
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE " & sConCol & " = '" & sConVal & "'"
End If
End Select
'do DB-Stuff
objConnection.open sConnectionString
objRecordset.Open dbQuery, objConnection
IF objRecordset.fields.Count = 1 Then
lTemp = objRecordset.fields(0)
End If
objRecordset.close
objConnection.close
dbCall = lTemp
End function
我的hta-file.
的设计会根据这些结果来决定一些事情。
我有多种用途。
像这样称呼它:
getSlotAmount = dbCall("get", "value", "config", "name", "MiPaCount")
returns一个数字根据
name (short String)
value (Integer)
MiPaCount
5
但是如果我用
调用它
iStart = dbCall("get", "startzeit", "slots", "ID", tmp)
我从上面得到错误。
table 目前看起来像
ID (Integer, Byte)
Startzeit (short String)
1
11:30
以前“startzeit”列是 time-type 但因为我只是在数据库中存储而不是计算数据所以它并不那么重要
我尝试使用一个整数和一个字符串作为 tmp 但在所有这些情况下,它在我尝试 objRecordset.open 的行上给我一个错误(德语:“Datentypenkonflikt in Kriterienausdruck”,将其翻译为 google导致标题)。
在创建这个问题时,SO 向我提供了多个类似的帖子,这些帖子让我进行了另外两个实验。
当我尝试像这样更改字符串“dbQuery = ...”时:
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE " & sConCol & " = '" & sConVal
它说缺少一个必需的值。
当我尝试这个时:
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE " & sConCol & " = """ & sConVal & """"
我再次从标题中得到错误,所以我返回到我的原始字符串(因为它适用于我使用 dbCall 的第一个示例)。
我错过了什么?
编辑:
根据接受的答案找到解决方案。使用此功能它可以工作:
function dbCall(sAction, sPayload, sTable, conCol, conVal, conType)
updateLocalDB()
Dim sConnectionString, objConnection, objRecordset, dbQuery, lTemp
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & localDB
'Query für die DB zusammenbauen
Select Case sAction
Case "get"
dbQuery = "SELECT " & sPayload & " FROM " & sTable
End Select
Select Case conType
Case "str"
dbQuery = dbQuery & " WHERE " & ConCol & " = '" & ConVal & "'"
Case "int"
dbQuery = dbQuery & " WHERE " & ConCol & " = " & ConVal
End Select
'do DB-Stuff
objConnection.open sConnectionString
objRecordset.Open dbQuery, objConnection
IF objRecordset.fields.Count = 1 Then
lTemp = objRecordset.fields(0)
End If
objRecordset.close
objConnection.close
dbCall = lTemp
End function
您不知道您希望处理多种数据类型,并处理字段名称的保留字。因此,例如,您需要:
' For text:
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE [" & sConCol & "] = '" & sConVal & "'"
' For numbers:
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE [" & sConCol & "] = " & Str(sConVal) & ""
' For dates:
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE [" & sConCol & "] = #" & Format(DateValue(sConVal), "yyyy\/mm\/dd") & "#"
您可以使用我的函数 来简化它,但我还没有在这样的场景中对其进行测试。
尝试替换此行:
iStart = dbCall("get", "startzeit", "slots", "ID", tmp)
有了这个:
iStart = dbCall("get", "startzeit", "slots", "ID", "tmp")
您正在该参数中寻找一个值,而 tmp(不带引号)应该是一个数字。
我目前正在尝试在我的 vbscript 中使用数据库 (.accdb-file)。
我的函数来自 .vbs-file,由 hta-file:
执行function dbCall(sAction, sPayload, sTable, sConCol, sConVal)
updateLocalDB()
Dim sConnectionString, objConnection, objRecordset, dbQuery, lTemp
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & localDB
'Query für die DB zusammenbauen
Select Case sAction
Case "get"
If sConCol = False Then
dbQuery = "SELECT " & sPayload & " FROM " & sTable
Else
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE " & sConCol & " = '" & sConVal & "'"
End If
End Select
'do DB-Stuff
objConnection.open sConnectionString
objRecordset.Open dbQuery, objConnection
IF objRecordset.fields.Count = 1 Then
lTemp = objRecordset.fields(0)
End If
objRecordset.close
objConnection.close
dbCall = lTemp
End function
我的hta-file.
的设计会根据这些结果来决定一些事情。我有多种用途。 像这样称呼它:
getSlotAmount = dbCall("get", "value", "config", "name", "MiPaCount")
returns一个数字根据
name (short String) | value (Integer) |
---|---|
MiPaCount | 5 |
但是如果我用
调用它iStart = dbCall("get", "startzeit", "slots", "ID", tmp)
我从上面得到错误。 table 目前看起来像
ID (Integer, Byte) | Startzeit (short String) |
---|---|
1 | 11:30 |
以前“startzeit”列是 time-type 但因为我只是在数据库中存储而不是计算数据所以它并不那么重要 我尝试使用一个整数和一个字符串作为 tmp 但在所有这些情况下,它在我尝试 objRecordset.open 的行上给我一个错误(德语:“Datentypenkonflikt in Kriterienausdruck”,将其翻译为 google导致标题)。 在创建这个问题时,SO 向我提供了多个类似的帖子,这些帖子让我进行了另外两个实验。
当我尝试像这样更改字符串“dbQuery = ...”时:
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE " & sConCol & " = '" & sConVal
它说缺少一个必需的值。 当我尝试这个时:
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE " & sConCol & " = """ & sConVal & """"
我再次从标题中得到错误,所以我返回到我的原始字符串(因为它适用于我使用 dbCall 的第一个示例)。 我错过了什么?
编辑: 根据接受的答案找到解决方案。使用此功能它可以工作:
function dbCall(sAction, sPayload, sTable, conCol, conVal, conType)
updateLocalDB()
Dim sConnectionString, objConnection, objRecordset, dbQuery, lTemp
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & localDB
'Query für die DB zusammenbauen
Select Case sAction
Case "get"
dbQuery = "SELECT " & sPayload & " FROM " & sTable
End Select
Select Case conType
Case "str"
dbQuery = dbQuery & " WHERE " & ConCol & " = '" & ConVal & "'"
Case "int"
dbQuery = dbQuery & " WHERE " & ConCol & " = " & ConVal
End Select
'do DB-Stuff
objConnection.open sConnectionString
objRecordset.Open dbQuery, objConnection
IF objRecordset.fields.Count = 1 Then
lTemp = objRecordset.fields(0)
End If
objRecordset.close
objConnection.close
dbCall = lTemp
End function
您不知道您希望处理多种数据类型,并处理字段名称的保留字。因此,例如,您需要:
' For text:
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE [" & sConCol & "] = '" & sConVal & "'"
' For numbers:
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE [" & sConCol & "] = " & Str(sConVal) & ""
' For dates:
dbQuery = "SELECT " & sPayload & " FROM " & sTable & " WHERE [" & sConCol & "] = #" & Format(DateValue(sConVal), "yyyy\/mm\/dd") & "#"
您可以使用我的函数
尝试替换此行:
iStart = dbCall("get", "startzeit", "slots", "ID", tmp)
有了这个:
iStart = dbCall("get", "startzeit", "slots", "ID", "tmp")
您正在该参数中寻找一个值,而 tmp(不带引号)应该是一个数字。