为什么sp_send_dbmail继续发送电子邮件?

时间:2018-07-11 08:08:46

标签: loops sql-server-2008 stored-procedures vb.net-2010

首先,使用此method已解决了问题。

但是这个问题已经解决,但不知道为什么sp_send_dbmail会继续发送电子邮件。

我的情况。

我有一个程序可以检查整个excel工作表并检查其内容,如果在excel工作表中发现任何错误,该程序将触发并向相关人员发送电子邮件。

在测试期间,该过程运行良好。每个错误仅发送一封邮件。如果工作表中有5个错误,则系统将发送5封有关所述错误的电子邮件。这很关键,因为这里的错误是高优先级的错误,应该总是完美的。

然后,我添加了代码以获取详细信息。在测试过程中,这里没有发送电子邮件。我发现我的程序正在不停地发送电子邮件。如果我还没有删除排队的电子邮件,则可能还会发送4,000多封电子邮件。好东西,我只是在使用测试服务器。目前,我正在检查可能引起该现象的情况。我可以使用什么对策?

我必须为此发布一个很长的代码。

如果需要任何其他信息,请问我。

'My Main procedure
    Sub CheckDataOfExcel(ByVal locs As String)

        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet

        Dim location As String
        Dim isData As Boolean = False
        Dim m_CountTo As Integer = 0
        'Progress Bar STEP
        Dim PBStep As Integer

        filename = ""
        globalloc = ""
        'Gets Files from Folders
        Dim counting As Integer = 0
        Dim csvcount As String() = Directory.GetFiles(locs, "*.xls")
        Dim ToUploadCSV As String



        For counting = 0 To csvcount.Count - 1
            CheckListofCSV.Clear()
            filename = ""

            location = csvcount(counting).ToString
            globalloc = location

            ToUploadCSV = Path.GetFileName(location).ToString
            Dim ListLines As New List(Of String)
            Dim ListLinesNEW As New List(Of String)


            'Read the file


            'If data is retrieved
            xlApp = New Excel.Application
            xlWorkBook = xlApp.Workbooks.Open(location)
            xlWorkSheet = xlWorkBook.Worksheets("Sheet1")
            'Use this code to read the data from EXCEL

            'Check if file is already uploaded
            If ShowExclUpld(xlWorkSheet.Cells(62, 2).value, xlWorkSheet.Cells(3, 2).value, xlWorkSheet.Cells(4, 2).value) <> "" Then
                 MsgBox("A file named " & ShowExclUpld(xlWorkSheet.Cells(62, 2).value, xlWorkSheet.Cells(3, 2).value, xlWorkSheet.Cells(4, 2).value) & " was uploaded containing similar data of " & ToUploadCSV & " please check.")

            Else




                'ROWS
                Dim i As Integer = 5
                'COLUMNS
                Dim col As Integer = 2
                'SAMPLING COUNT FROM EXCEL
                Dim SampQTYXcel As Integer = 0
                'below code to check data only
                Do While Len(xlWorkSheet.Cells(i, col).value) > 0

                    '**DATA DETAILS**
                    'Loop starts here
                    'Product Property Sub Header
                    For i = 5 To 10

                        'Check Dimension Here
                        'If Spec is correct, do nothing.
                        'I think I should just return the list of all the PPF,Part number,Dimension that 
                        'Create procedure to check SPecs? then get the file name and file location
                        'along with the PPFNo, PartNo, and Dimension name

                        checkSpecData(xlWorkSheet.Cells(3, 2).value, xlWorkSheet.Cells(5, col).value, xlWorkSheet.Cells(62, 2).value, _
                        ToUploadCSV, location, xlWorkSheet.Cells(6, col).value, xlWorkSheet.Cells(7, col).value, xlWorkSheet.Cells(8, col).value, i)

                        'Now I should get a flag to tell the system not to upload this filename anymore...


                    Next

                    'TASK:Create POKAYOKE for Sample Size Checking
                    '1) Encoded Sample Size my be within the upper and lower limit specification
                    '2) Encoded Sample Size encoded must be equal to the Sample Size requirement encode from the database

                    'Use these data for POKAYOKE regarding Specification upper and lower limits
                    Dim XLUprLmt As Decimal = xlWorkSheet.Cells(7, col).value
                    Dim XLLwrLmt As Decimal = xlWorkSheet.Cells(8, col).value

                    If ExlErrorFlag = ToUploadCSV Then
                        'Sample Data 10 onwards
                        Do While Len(xlWorkSheet.Cells(i, col).value) > 0
                            SampQTYXcel = SampQTYXcel + 1

                            If xlWorkSheet.Cells(i, col).value > XLUprLmt Or xlWorkSheet.Cells(i, col).value < XLLwrLmt Then

                                MsgBox(createEmailMsgForSamplErr(SampQTYXcel, xlWorkSheet.Cells(i, col).value))

                            End If



                            i = i + 1

                        Loop

                        'Excel data has more data than Database QTY then REPORT 
                        If SampQTYXcel > SamplQTY Then
                                  MsgBox("ERROR")
                        End If

                        'Add 1 to move to next column
                        col = col + 1
                        i = 5
                        SampQTYXcel = 0
                    End If
                Loop

                xlApp.DisplayAlerts = False

                xlWorkBook.Save()

            End If




            releaseObject(xlWorkSheet.UsedRange)
            releaseObject(xlWorkSheet)
            xlWorkBook.Close()
            releaseObject(xlWorkBook)
            xlApp.Quit()
            releaseObject(xlApp)

            xlApp = Nothing
            xlWorkBook = Nothing
            xlWorkSheet = Nothing

            Cleanup()

        Next

    End Sub

SP

GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Alter procedure [dbo].[checkData]
@PartNo as nvarchar(20),
@DimName as nvarchar(20),
@PPFNo as nvarchar(20),
@FileName as nvarchar(20),
@FileLocation as nvarchar(150),
@SpectoTest as nvarchar(20),
@UprLmtToTest as nvarchar(10),
@LwrLmtToTest as nvarchar(10) ,
@flag as integer

as

begin
Declare @TheEmailBody as nvarchar(max)
if @flag = 5
    begin
        if cast(@SpectoTest as decimal(18,4)) <>  (select cast(specification as decimal(18,4)) from DimensionMaster where PartNo = @PartNo and DimensionName = @DimName)

            begin

                set @TheEmailBody = '
---------------------------------------------------------------------------------------------------------------------------------
Part Number         : '+@PartNo+      '
Dimension Name: '+@DimName+    '
PPF Number          : '+@PPFNo+       '
File Name               : '+@FileName+    '
File Location          : '+@FileLocation+'

Error: Different Specification

Specification from EXCEL: '+@SpectoTest+' 
Specification from DB: '+ (select specification from DimensionMaster where PartNo = @PartNo and DimensionName = @DimName) +'

This is a system generated email
---------------------------------------------------------------------------------------------------------------------------------
'


                    exec msdb.dbo.sp_send_dbmail 
                    @profile_name = 'SendSQLMail', 
                    @recipients = 'someone@somewhere.com', 
                    @subject = 'TEST', 
                    @body =  @TheEmailBody,
                    @body_format = 'text'

                    select @FileName
            end
    end


else if @flag = 9
    begin

        if cast(@UprLmtToTest as decimal(18,4)) <> (select cast(UpperLimit as decimal(18,4)) + cast(specification as decimal(18,4))  from DimensionMaster where PartNo = @PartNo and DimensionName = @DimName)

            begin
set @TheEmailBody = '
---------------------------------------------------------------------------------------------------------------------------------
Part Number         : '+@PartNo+      '
Dimension Name: '+@DimName+    '
PPF Number          : '+@PPFNo+       '
File Name               : '+@FileName+    '
File Location          : '+@FileLocation+'

Error: Different UpperLimit

UpperLimit from EXCEL: '+@UprLmtToTest+' 
UpperLimit from DB: '+ cast((select cast(UpperLimit as decimal(18,3)) + cast(specification as decimal(18,3)) from DimensionMaster where PartNo = @PartNo and DimensionName = @DimName) as nvarchar(max)) +'

This is a system generated email
---------------------------------------------------------------------------------------------------------------------------------
'

            exec msdb.dbo.sp_send_dbmail 
            @profile_name = 'SendSQLMail', 
            @recipients = 'someone@somewhere.com', 
            @subject = 'TEST', 
            @body = @TheEmailBody , 
            @body_format = 'text'

            select @FileName

            end

    end


else if @flag = 10

    begin
        if cast(@LwrLmtToTest as decimal(18,4)) <> (select cast(Specification as decimal(18,4)) - cast(lowerlimit as decimal(18,4))  from DimensionMaster where PartNo = @PartNo and DimensionName = @DimName)

            begin
set @TheEmailBody = '
---------------------------------------------------------------------------------------------------------------------------------
Part Number         : '+@PartNo+      '
Dimension Name: '+@DimName+    '
PPF Number          : '+@PPFNo+       '
File Name               : '+@FileName+    '
File Location          : '+@FileLocation+'

Error: Different LowerLimit

LowerLimit from EXCEL: '+@LwrLmtToTest+' 
LowerLimit from DB: '+ cast((select cast(Specification as decimal(18,3)) - cast(lowerlimit as decimal(18,3)) from DimensionMaster where PartNo = @PartNo and DimensionName = @DimName) as nvarchar(max)) +'

This is a system generated email
---------------------------------------------------------------------------------------------------------------------------------
'
            exec msdb.dbo.sp_send_dbmail 
            @profile_name = 'SendSQLMail', 
            @recipients = 'someone@somewhere.com', 
            @subject = 'TEST', 
            @body = @TheEmailBody, 
            @body_format = 'text'

            select @FileName

            end
    end


end

0 个答案:

没有答案