我有3个radioButtons(" YES"," NO"," UNKNOWN"),它与我的数据库中的列匹配3个可能的值(1,0 , 空值)。
当选择的radioButton是" UNKNOWN"我想在此表中插入一个NULL值(使用Variant类型)。但是当我尝试使用ADODB连接运行SQL查询时,它会返回错误。
有没有在我的数据库中传递NULL值的技巧?这是我的代码的副本:
Public Function setCandidature(idC As Integer, idO As Integer, nomC As String, prenomC As String, nomM As String, prenomM As String, idRegion As Integer, idUM As Integer, idDUM As Integer, nni As String, emploiC As String, repM As Integer, repA As Integer, accDisp As Integer, precAcc1 As Integer, precAcc2 As Integer)
Dim sqlQuery As String
Dim rs As ADODB.Recordset
Dim repAVar As Variant, repMVar As Variant
Set connect = New ADODB.Connection
connect.Open connString
If repA = -1 Then
repAVar = Null
Else
repAVar = repA
End If
If repM = -1 Then
repMVar = Null
Else
repMVar = repM
End If
sqlQuery = "UPDATE candidatures SET offre_ID = " & idO & ", candidat_Nom = " & nomC & ", candidat_Prenom = " & prenomC & ", manager_Nom = " & nomM & ", manager_Prenom = " & prenomM & ", reponse_Manager = " & repMVar & ", reponse_Agent = " & repAVar & ", region_Candidat = " & idRegion & _
", um_Candidat = " & idUM & ", dum_Candidat = " & idDUM & ", nni_Candidat = " & nni & ", emploi_Candidat = " & emploiC & ", accompagnement_Dispense = " & accDisp & ", accompagnement_Precision_ID = " & precAcc1 & ", accompagnement2_Precision_ID = " & precAcc2 & _
" WHERE candidature_ID = " & idC & ";"
rs.Open sqlQuery, connect
End Function
我首先将我想要为NULL的值作为包含-1的整数传递。
答案 0 :(得分:5)
您想要的SQL语句最终如下所示:
UPDATE candidatures
SET offre_ID = 42
,candidat_Nom = 'Doe'
,candidat_Prenom = 'John'
,manager_Nom = 'Nuts'
,manager_Prenom = 'Doug'
,reponse_Manager = NULL
,reponse_Agent = NULL
,region_Candidat = 'WEST'
,um_Candidat = 72
,dum_Candidat = 72
,nni_Candidat = 24
,emploi_Candidat = 'something'
,accompagnement_Dispense = 'whatever'
,accompagnement_Precision_ID = 10
,accompagnement2_Precision_ID = 11
WHERE candidature_ID = 2345;
您希望将字符串括在单引号中,将不的数值括在这样的单引号中,并按字面指定NULL
值。
您可以保留字符串连接方法,并通过将NULL
字符串文字连接到查询中来使查询包含"NULL"
值:
If repA = -1 Then
repAVar = "NULL"
Else
repAVar = repA
End If
If repM = -1 Then
repMVar = "NULL"
Else
repMVar = repM
End If
显然这意味着要处理何时/是否包含单引号,何时不。
这可能会奏效......直到它没有:
UPDATE candidatures
SET offre_ID = 42
,candidat_Nom = 'O'Connor'
,candidat_Prenom = 'David'
...
我知道!我只是将字符串值中的任何单引号加倍!并且可能工作:
UPDATE candidatures
SET offre_ID = 42
,candidat_Nom = 'O''Connor'
,candidat_Prenom = 'David'
...
但是走下那座小山然后你就开始了...你会继续打补丁并“消毒”用户的输入,直到事情再次起作用,然后它们就会崩溃,你再次修补它......
有一种理智的方法可以做到这一点。
使用参数,让服务器处理参数。
因此,不是将参数值连接到命令字符串中,而是将其发送到服务器:
UPDATE candidatures
SET offre_ID = ?
,candidat_Nom = ?
,candidat_Prenom = ?
,manager_Nom = ?
,manager_Prenom = ?
,reponse_Manager = ?
,reponse_Agent = ?
,region_Candidat = ?
,um_Candidat = ?
,dum_Candidat = ?
,nni_Candidat = ?
,emploi_Candidat = ?
,accompagnement_Dispense = ?
,accompagnement_Precision_ID = ?
,accompagnement2_Precision_ID = ?
WHERE candidature_ID = ?;
..以及参数。
您不使用Recordset
。您使用Command
代替:
Dim connect As ADODB.Connection
Set connect = New ADODB.Connection
connect.ConnectionString = connString
connect.Open
With New ADODB.Command
.ActiveConnection = connect
.CommandType = adCmdText
.CommandText = sqlQuery
'append parameters in the same order they show up in the query
.Parameters.Append .CreateParameter(Type:=adInteger, Direction:=adParamInput, Value:=myValue)
'...
.Execute
End With
connect.Close
要为参数指定NULL
值,只需使用Null
关键字:
myValue = Null
这要求您的可以为空的值具有Variant
数据类型,因为VBA中的Integer
或Boolean
不能是Null
。
答案 1 :(得分:1)
只是对未来的想法 - 无论何时遇到像这样的大SQL代码的问题:
sqlQuery = "UPDATE candidatures SET offre_ID = " & idO & ", candidat_Nom = " & more and more more SQL here";"
执行以下操作:
debug.print sqlQuery
。编辑:如果你有time
& knowledge
& will to write good code
,不要写这样的代码。您可能会遇到SQL injection
和其他问题。而是使用Mat's Mug的答案中的方法。