3

我想知道是否有人可以帮助我。我对编程和开发非常陌生(因此,如果这是一个愚蠢的问题,我深表歉意),但我非常坚持我目前正在从事的项目。

我正在编写一个 WinForms 应用程序,该应用程序每天运行一次以从数据库中检索信息。由此,如果满足特定标准,它将自动向建筑物中的个人发送电子邮件。消息正文将包含带入数据网格的各种详细信息。

目前,我的应用程序在尝试将详细信息传输到负责生成电子邮件的子程序时崩溃(因为某些变量返回为“Null”,因此无法转换为字符串)。我正在尝试实现一个 If 语句,如果它返回为 Null,则将一个字符串分配给变量,如下所示:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles RunTimer.Tick

    years = My.Settings.Years.Split(",")

    If TimeOfDay = "09:00:00" Then
        Using cn As New SqlClient.SqlConnection
            cn.ConnectionString = constr
            cn.Open()

            SQLstr = "long SQL string that works as have run it in WinSQL"

            Using command As New SqlClient.SqlCommand(SQLstr, cn)
                command.CommandTimeout = 0
                command.CommandText = SQLstr
                Using drd As SqlClient.SqlDataReader = command.ExecuteReader
                    While drd.Read

                        If Not drd("Joined Date") Is DBNull.Value Then
                            Dim memberyears As Integer = DateDiff(DateInterval.Year, drd("Joined Date"), Now)
                            Dim element As Integer

                            For element = 0 To years.GetUpperBound(0)
                                If years(element) = memberyears Then
                                    Label1.Text = "Sending email"
                                End If
                                If Not drd("City") Is DBNull.Value Then
                                    drd("City") = "Bleh"
                                End If
                                SendEmail(years(element), drd("Shop Name"), drd("Contact Name"), drd("Shop ID"), drd("Business Name"), drd("Address 1"), drd("Address 2"), drd("Address 3"), drd("District"), drd("City"), drd("County"), drd("Shop Postal Code"), drd("ASM"))
                            Next
                        End If
                    End While
                End Using
            End Using
        End Using


    Else
        Label1.Text = "Skipped"
        Exit Sub
    End If

End Sub

If我正在处理的声明是这样的:

If Not drd("City") Is DBNull.Value Then
    drd("City") = "Bleh"
End If

但是它只是返回,因为属性“项目”是只读的。任何帮助将不胜感激。

4

2 回答 2

2

您不能更改 a 记录的字段SqlDataReaderDataReader仅用于读取数据。如果要修改它,可以使用DataTable/DataRow并使用它的IsNull方法来检查字段的值是否为空:

Using cn As New SqlClient.SqlConnection(constr)
    Using da = New SqlClient.SqlDataAdapter("long SQL string that works as have run it in WinSQL", cn)
        da.SelectCommand.CommandTimeout = 0
        Dim table = New DataTable()
        da.Fill(table)
        For Each row As DataRow In table.Rows
            If Not row.IsNull("Joined Date") Then
                Dim joined = row.Field(Of Date)("Joined Date")
                Dim memberyears As Long = DateDiff(DateInterval.Year, joined, Date.Now)
                For i As Int32 = 0 To years.Length - 1
                    If memberyears.ToString = years(i) Then
                        Label1.Text = "Sending email"
                        If Not row.IsNull("City") Then
                            row("City") = "Bleh"
                        End If
                        SendEmail(years(i), row("Shop Name"), row("Contact Name"), row("Shop ID"), row("Business Name"), row("Address 1"), row("Address 2"), row("Address 3"), row("District"), row("City"), row("County"), row("Shop Postal Code"), row("ASM"))
                    End If
                Next
            End If
        Next
    End Using
End Using
于 2013-04-06T13:12:15.397 回答
0

是的。它是一种read only财产,

对此的解决方案是让 avariable保存来自 的值database。但是,请注意这一点,在分配给 that 之前variable,我们必须检查它是否DbNull存在。

       Dim xCity as string = string.empty

       ....................
       ....................
       ....................

       xCity = if(IsDbNull(drd("City")),"some text","some other text")

然后将该变量xCity用于您将来的使用。

编辑:

如果假设你想避免那个额外的变量,那么你可以直接检查那个值并像这样传递一个有效的字符串,

SendEmail(**,**, if(IsDbNull(drd("City")),"some text","some other text"), **, **, **)
于 2013-04-06T13:06:25.703 回答