调用的目标抛出了异常。 ---> System.InvalidOperationException:行/列不存在任何数据

时间:2013-03-20 12:11:25

标签: sql vb.net oledb

我需要在那里使用SQL Server 2008在新服务器上迁移通知DTS包。我在VB.net中创建了脚本,以便我可以将其作为SSIS脚本任务放在那里。我收到以下错误: System.InvalidOperationException:行/列没有数据

我的代码如下:

JobRuningCon = New OleDbConnection(connstr)
JobRuningCon.Open()

' Query to check whether there are any failed jobs for the client
SQL1 = "select message,  [LastRunDate] = convert(varchar, cast(cast([msdb]..sysjobhistory.RUN_DATE as varchar) as datetime), 101),[LastRunTime] =STUFF(STUFF(RIGHT('000000'+CONVERT(VARCHAR(8), [msdb]..sysjobhistory.RUN_TIME), 6), 5, 0, ':'), 3, 0, ':'),[RunDuration] =STUFF(STUFF(RIGHT('000000'+CONVERT(VARCHAR(8), [msdb]..sysjobhistory.RUN_DURATION), 6), 5, 0, ':'), 3, 0, ':')  from msdb..sysjobhistory WITH (NOLOCK) where (step_name like " & Chr(39) & ClientName(i) & Chr(37) & "Nightly Update" & Chr(39) & " OR step_name like " & Chr(39) & ClientName(i) & Chr(37) & "ACN_CIRCULAR_MANAGER" & Chr(39) & ")" & " and run_date like" & Chr(39) & runDate & Chr(39) & ""

JobRuningCom = New OleDbCommand(SQL1, JobRuningCon)
JobRuningComReader = JobRuningCom.ExecuteReader
JobRuningCon.Close()

JobFailureCon = New OleDbConnection(connstr)
JobFailureCon.Open()

' Query to check whether there is any history for tonights job.. if it doesnt exists it might  mean the job is still running
SQL2 = "select step_name,message, LastRunDate = convert(varchar, cast(cast([msdb]..sysjobhistory.RUN_DATE as varchar) as datetime), 101), LastRunTime =STUFF(STUFF(RIGHT('000000'+CONVERT(VARCHAR(8), [msdb]..sysjobhistory.RUN_TIME), 6), 5, 0, ':'), 3, 0, ':'), RunDuration =STUFF(STUFF(RIGHT('000000'+CONVERT(VARCHAR(8), [msdb]..sysjobhistory.RUN_DURATION), 6), 5, 0, ':'), 3, 0, ':')  from msdb..sysjobhistory WITH (NOLOCK)  where  (step_name like " & Chr(39) & ClientName(i) & Chr(37) & "Nightly Update" & Chr(39) & " OR step_name like " & Chr(39) & ClientName(i) & Chr(37) & "ACN_CIRCULAR_MANAGER" & Chr(39) & ")" & "and run_date like" & Chr(39) & runDate & Chr(39) & "and run_status=0"

JobFailureCom = New OleDbCommand(SQL2, JobFailureCon)
JobFailureComReader = JobFailureCom.ExecuteReader
JobRuningCon.Close()

JobSuccessCon = New OleDbConnection(connstr)
JobSuccessCon.Open()

' Query to check whether there are any successful jobs for the client
SQL3 = "select step_name,message,  convert(varchar, cast(cast([msdb]..sysjobhistory.RUN_DATE as varchar) as datetime), 101) as LastRunDate, LastRunTime =STUFF(STUFF(RIGHT('000000'+CONVERT(VARCHAR(8), [msdb]..sysjobhistory.RUN_TIME), 6), 5, 0, ':'), 3, 0, ':'), RunDuration =STUFF(STUFF(RIGHT('000000'+CONVERT(VARCHAR(8), [msdb]..sysjobhistory.RUN_DURATION), 6), 5, 0, ':'), 3, 0, ':')  from msdb..sysjobhistory WITH (NOLOCK) where step_name like " & Chr(39) & ClientName(i) & Chr(37) & "Nightly Update" & Chr(39) & " and run_date like" & Chr(39) & runDate & Chr(39) & "and run_status=1"

JobSuccessCom = New OleDbCommand(SQL3, JobSuccessCon)
JobSuccessComReader = JobSuccessCom.ExecuteReader
JobSuccessCon.Close()

ServerNameCon = New OleDbConnection(connstr)
ServerNameCon.Open()
SQLServerName = "select @@servername as sname"

ServerNameCom = New OleDbCommand(SQL3, ServerNameCon)
ServerNameComReader = ServerNameCom.ExecuteReader
ServerNameCon.Close()

PrevFailCountCon = New OleDbConnection(connstr)
PrevFailCountCon.Open()
SQL4 = "select  count(distinct RUN_DATE) as failcount  from sysjobhistory WITH (NOLOCK) where step_name like " & Chr(39) & ClientName(i) & Chr(37) & " nightly update" & Chr(39) & " and run_date>(select max(run_date)from sysjobhistory where step_name like " & Chr(39) & ClientName(i) & Chr(37) & " nightly update" & Chr(39) & " and run_status=1)"

PrevFailCountCom = New OleDbCommand(SQL4, PrevFailCountCon)
PrevFailCountComReader = PrevFailCountCom.ExecuteReader

PrevFailCountCon.Close()
LastSuccessCon = New OleDbConnection(connstr)

LastSuccessCon.Open()
SQL5 = "select convert(varchar, cast(cast([msdb]..sysjobhistory.RUN_DATE as varchar) as datetime), 101) as LastRunDate, LastRunTime =STUFF(STUFF(RIGHT('000000'+CONVERT(VARCHAR(8), [msdb]..sysjobhistory.RUN_TIME), 6), 5, 0, ':'), 3, 0, ':'), RunDuration =STUFF(STUFF(RIGHT('000000'+CONVERT(VARCHAR(8), [msdb]..sysjobhistory.RUN_DURATION), 6), 5, 0, ':'), 3, 0, ':')  from msdb..sysjobhistory WITH (NOLOCK) where step_name like " & Chr(39) & ClientName(i) & Chr(37) & "Nightly Update" & Chr(39) & " and run_date = (select MAX(run_date) from msdb..sysjobhistory WITH (NOLOCK) where run_status=1and step_name like" & Chr(39) & ClientName(i) & Chr(37) & "Nightly Update" & Chr(39) & ")"

LastSuccessCom = New OleDbCommand(SQL5, LastSuccessCon)
LastSuccessComReader = LastSuccessCom.ExecuteReader

LastSuccessCon.Close()
Failure_20Con = New OleDbConnection(connstr)
Failure_20Con.Open()

SQL6 = "(select count(*) as No_Of_Failure from msdb..sysjobhistory WITH (NOLOCK) where step_name like " & Chr(39) & ClientName(i) & Chr(37) & "Nightly Update" & Chr(39) & ")"

Failure_20Com = New OleDbCommand(SQL6, Failure_20Con)
Failure_20comReader = Failure_20Com.ExecuteReader

Failure_20Con.Close()

Fail_20 = Failure_20comReader.Item("No_Of_Failure")
JobCompletionStatus = ""

While JobRuningComReader.Read
     JobCompletionStatus = JobRuningComReader.Item("message")
End While

' While LastSuccessComReader.Read
' LastDate = LastSuccessComReader.Item("LastRunDate") & "  " & LastSuccessComReader.Item("LastRunTime")
'End While

LastDate = LastSuccessComReader.Item("LastRunDate") & "  " & LastSuccessComReader.Item("LastRunTime")

'Check whether the job has failed more than 20 days
If Fail_20 = 0 Then
    JobStatus = "<p><PRE><font face=Verdana size=1><b> " & ClientName(i) & "</b>" & "<font Color=RED face=Verdana size=2 >  " & " This job has failed more than 20 days.Please Check </br> </PRE></font></font></p>"

' Check whether the job is still running or not. If there are no records for a particular day, it means the job is still running.
ElseIf JobCompletionStatus = "" Then
    JobStatus = "<p><PRE><font face=Verdana size=1><b> " & ClientName(i) & "</b>" & "<font Color=RED face=Verdana size=1>  " & " :There is no record for this job today. The job might have failed to start or might be still running. Please Check. </br> The Last Successful run was on " & LastDate & " </PRE></font></font></p>"
    CountInProgress = CountInProgress + 1

Else
    ' If there is a record then check whether the job is a failure or not
    JobFailureStatus = ""

    While JobFailureComReader.Read
        LastRunDate1 = JobFailureComReader.Item("LastRunDate")
        LastRunTime1 = JobFailureComReader.Item("LastRunTime")
        RunDuration1 = JobFailureComReader.Item("RunDuration")

        While PrevFailCountComReader.Read
            prefailCount = PrevFailCountComReader.Item("failcount")
        End While

        JobFailureStatus = "<p><b><PRE><font face=Verdana size=1>  " & ClientName(i) & "</b>         " & LastRunDate1 & "    " & LastRunTime1 & "          " & RunDuration1 & "        " & "<font Color=RED> FAILED              </font>" & prefailCount & "</br>" & "</br></p><p></br><font Color=RED size=2> The job failed at step: " & JobFailureComReader.Item("step_name") & "" & "  Message: " & "<font Color=RED size=2> Please Check.</PRE></font></p><p>The Last Successful run was on " & LastDate & " </p>"
        CountFailure = CountFailure + 1

    End While
    JobStatus = JobFailureStatus

    If JobFailureStatus = "" Then
        ' Checks whether the job was a success or not.
        While JobSuccessComReader.Read
            LastRunDate1 = JobSuccessComReader.Item("LastRunDate")
            LastRunTime1 = JobSuccessComReader.Item("LastRunTime")
            RunDuration1 = JobSuccessComReader.Item("RunDuration")

            JobStatus = "<p><b><PRE><font face=Verdana size=1>  " & ClientName(i) & "</b>           " & LastRunDate1 & "    " & LastRunTime1 & "          " & RunDuration1 & "            " & "<font Color=green> SUCCEEDED</font>        NA</font></PRE></p>"

            CountSuccess = CountSuccess + 1
        End While
        JobStatus = JobStatus
    End If
End If

0 个答案:

没有答案