从 Excel 的 vba 中通过 ADO 执行 msdb.dbo.sp_send_dbmail
Executing msdb.dbo.sp_send_dbmail via ADO from within Excel's vba
我正在尝试 运行 以下内容:
Sub sendMailViaADO()
Dim cmdStoredFunct As ADODB.Command
Dim r As ADODB.Recordset
Dim strConn As String
strConn = _
"PROVIDER=SQLOLEDB.1;" & _
"P-----D=xxxxx;" & _
"PERSIST SECURITY INFO=True;" & _
"USER ID=yyyyyy;" & _
"INITIAL CATALOG=xxxxx;" & _
"DATA SOURCE=xxxxx;" & _
"USE PROCEDURE FOR PREPARE=1;" & _
"AUTO TRANSLATE=True;" & _
"CONNECT TIMEOUT=0;" & _
"COMMAND TIMEMOUT=0" & _
"PACKET SIZE=4096;" & _
"USE ENCRYPTION FOR DATA=False;" & _
"TAG WITH COLUMN COLLATION WHEN POSSIBLE=False"
Set c= New ADODB.Connection
c.ConnectionString = strConn
c.CommandTimeout = 0
c.Open
Set cmdStoredFunct = New ADODB.Command
Set cmdStoredFunct.ActiveConnection = c
Set r = New ADODB.Recordset
With cmdStoredFunct
.CommandText = "msdb..sp_send_dbmail"
.CommandText = adCmdStoredProc
.Parameters.Append .CreateParameter("@recipients", adVarWChar, adParamInput, 50, "me@me.co.uk;")
.Parameters.Append .CreateParameter("@subject", adVarWChar, adParamInput, 50, "xxx")
.Parameters.Append .CreateParameter("@body", adVarWChar, adParamInput, 50, "yyy")
Set r = .Execute
End With
'>>>
'>>>is there another way like the following ?
'Dim sTemp1
'sTemp1 = "{call msdb.dbo.sp_send_dbmail('me@me.co.uk', 'xxx', 'yyy')}"
'>>>
If Not (cmdStoredFunct Is Nothing) Then
Set cmdStoredFunct.ActiveConnection = Nothing
Set cmdStoredFunct = Nothing
End If
If Not (c Is Nothing) Then
If (c.State And 1) = 1 Then c.Close
End If
End Sub
执行命令 Set r = .Execute
时出错,并显示以下消息:
在我看来它正在找到存储过程,但它使用的字符串导致了这个神秘的异常。
我该如何解决这个问题?
例如,如果您想避免使用命令对象:
Dim sSQL as String
sSQL =
"EXEC msdb.dbo.sp_send_dbmail " & _
@recipients='me@me.co.uk'," & _
@subject='xxx'," & _
@body='yyy'"
Call r.Open(sSQL,c)
人们会对 SQL 注射感到气愤,但这是您最不担心的事情。
我正在尝试 运行 以下内容:
Sub sendMailViaADO()
Dim cmdStoredFunct As ADODB.Command
Dim r As ADODB.Recordset
Dim strConn As String
strConn = _
"PROVIDER=SQLOLEDB.1;" & _
"P-----D=xxxxx;" & _
"PERSIST SECURITY INFO=True;" & _
"USER ID=yyyyyy;" & _
"INITIAL CATALOG=xxxxx;" & _
"DATA SOURCE=xxxxx;" & _
"USE PROCEDURE FOR PREPARE=1;" & _
"AUTO TRANSLATE=True;" & _
"CONNECT TIMEOUT=0;" & _
"COMMAND TIMEMOUT=0" & _
"PACKET SIZE=4096;" & _
"USE ENCRYPTION FOR DATA=False;" & _
"TAG WITH COLUMN COLLATION WHEN POSSIBLE=False"
Set c= New ADODB.Connection
c.ConnectionString = strConn
c.CommandTimeout = 0
c.Open
Set cmdStoredFunct = New ADODB.Command
Set cmdStoredFunct.ActiveConnection = c
Set r = New ADODB.Recordset
With cmdStoredFunct
.CommandText = "msdb..sp_send_dbmail"
.CommandText = adCmdStoredProc
.Parameters.Append .CreateParameter("@recipients", adVarWChar, adParamInput, 50, "me@me.co.uk;")
.Parameters.Append .CreateParameter("@subject", adVarWChar, adParamInput, 50, "xxx")
.Parameters.Append .CreateParameter("@body", adVarWChar, adParamInput, 50, "yyy")
Set r = .Execute
End With
'>>>
'>>>is there another way like the following ?
'Dim sTemp1
'sTemp1 = "{call msdb.dbo.sp_send_dbmail('me@me.co.uk', 'xxx', 'yyy')}"
'>>>
If Not (cmdStoredFunct Is Nothing) Then
Set cmdStoredFunct.ActiveConnection = Nothing
Set cmdStoredFunct = Nothing
End If
If Not (c Is Nothing) Then
If (c.State And 1) = 1 Then c.Close
End If
End Sub
执行命令 Set r = .Execute
时出错,并显示以下消息:
在我看来它正在找到存储过程,但它使用的字符串导致了这个神秘的异常。
我该如何解决这个问题?
例如,如果您想避免使用命令对象:
Dim sSQL as String
sSQL =
"EXEC msdb.dbo.sp_send_dbmail " & _
@recipients='me@me.co.uk'," & _
@subject='xxx'," & _
@body='yyy'"
Call r.Open(sSQL,c)
人们会对 SQL 注射感到气愤,但这是您最不担心的事情。