1

我知道有关于此的类似帖子,但我似乎找不到任何有用的东西。

我还是 VB 的新手,这可能很明显让我发疯。

我想做的是运行 SQL SELECT 并将结果通过电子邮件发送给需要它的人。

当我运行它时,我收到一封“ELSE”电子邮件,其中指出没有任何内容处于 HOLD 状态,但是在 SQL Server Management Studio 中运行查询确实会显示结果。

这是我所拥有的:

Public Sub Main()
    Dim connection As New SqlConnection(My.Settings.connectionString)
    connection.Open()
    Dim sqlCommand As SqlCommand
    log(" Processing - Searching SQL Database for Held ONLN Jobs ")

 Dim sqlQ1 As String = "Select [JobID] FROM [cemail].[dbo].[JobTb] " +
               "WHERE ApplicationID = 7 AND Status = 10 ORDER BY [JobID]"

    sqlCommand1 = New SqlCommand(sqlQ1, connection)
    Dim result = sqlCommand1.ExecuteScalar()

   Dim sqlQ2 As String = "Select [JobID],[JobNumber],[ApplicationID],[GeneratedDate]," +
                "[ReceivedDate],[CompletedDate],[Status],[ExpectedRecordNumber]," +
                "[ReceivedRecordNumber],[BadRecordNumber] " +
                "FROM [cemail].[dbo].[JobTb] " +
                "WHERE ApplicationID = 7 AND Status = 10"
    sqlCommand2 = New SqlCommand(sqlQ2, connection)
    Dim reader As SqlDataReader = sqlCommand2.ExecuteReader()
    While reader.Read()
        Dim jobID = reader(0)
        Dim jobNumber = reader(1)
        Dim appID = reader(2)
    End While

    If (result > 0) Then

 SendEmail(My.Settings.emailuser1, "Current Held Jobs", "Hello," & jobID & "Kind Ragards" )
    Else

        SendEmail(My.Settings.emailuser1, "No Jobs on Hold", "There are no Jobs Currently on Hold" )


    End If


    connection.Close()

    log("Finished")
End Sub

对此的任何帮助将不胜感激。提前致谢。

4

2 回答 2

3

传递 Select 语句时不应调用ExecuteNonQuery 。
从MSDN上的评论

对于 UPDATE、INSERT 和 DELETE 语句,返回值是受命令影响的行数。......... 对于所有其他类型的语句,返回值为 -1.......

您可以通过这种方式更改语句以简化查询

Dim sqlQ As String = "Select count(*) as recFound FROM [cemail].[dbo].[JobTb] " + 
                     "WHERE ApplicationID = 7 AND Status = 10"

sqlCommand = New SqlCommand(sqlQ, connection)
Dim result = sqlCommand.ExecuteScalar()

此外,在上面的代码中,不需要使用事务。

编辑看到您的评论,我还将展示使用SqlDataReader从数据库中读取值的部分

Dim sqlQ As String = "Select [JobID],[JobNumber],[ApplicationID],[GeneratedDate]," +
                    "[ReceivedDate],[CompletedDate],[Status],[ExpectedRecordNumber]," + 
                    "[ReceivedRecordNumber],[BadRecordNumber] " + 
                    "FROM [cemail].[dbo].[JobTb] " + 
                    "WHERE ApplicationID = 7 AND Status = 10"

sqlCommand = New SqlCommand(sqlQ, connection)
Dim reader As SqlDataReader = sqlCommand.ExecuteReader()
While reader.Read()
     Dim jobID = reader(0)
     Dim jobNumber = reader(1)
     Dim appID = reader(2)
     ..... 'and so on'
End While 
于 2013-01-23T11:19:08.550 回答
0

我认为问题在于您想要执行查询但正在使用ExecuteNonQuery()。尝试使用这样的SqlDataAdapter

Public Function SelectSqlSrvRows(dataSet As DataSet, connection As String, query As String) As DataSet
    Dim conn As New SqlConnection(connection)
    Dim adapter As New SqlDataAdapter()
    adapter.SelectCommand = new SqlCommand(query, conn)
    adapter.Fill(dataset)
    Return dataset
End Function
于 2013-01-23T11:20:21.043 回答