Object initializing with db null check

风格不统一 提交于 2019-12-13 03:59:26

问题


I am trying to initialize an object with data from database return in a dataset as below:

Class Pdf
    Public FileId As Integer
    Public AccountNumber As Integer
    Public DateSaved As DateTime
    Public FileName As String
    Public DateImported As DateTime

Scenerio 1 I can intialize the object like this:

Dim pdf = New Pdf With {.FileId = ds.Tables(0).Rows(i)("fileid"),
                        .AccountNumber = ds.Tables(0).Rows(i)("accountnumber"),
                        .DateSaved = ds.Tables(0).Rows(i)("datesaved"),
                        .FileName = ds.Tables(0).Rows(i)("filename"),
                        .DateImported = ds.Tables(0).Rows(i)("dateimported")
                        }

But this is not working, because column data can be null and I am not if how to do a db null check in this approach.

Then I have scenerio 2:

Dim pdf As New pdf
If Not IsDBNull(ds.Tables(0).Rows(i)("fileid")) Then
    PdfFileId = ds.Tables(0).Rows(i)("fileid")
Else
    PdfFileId = 0
End If

If Not IsDBNull(ds.Tables(0).Rows(i)("accountnumber")) Then
    pdf.AccountNumber = ds.Tables(0).Rows(i)("accountnumber")
Else
    pdf.AccountNumber = 0
End If

If Not IsDBNull(ds.Tables(0).Rows(i)("datesaved")) Then
    pdf.DateSaved = Format(ds.Tables(0).Rows(i)("datesaved"), "yyyy-MM-dd")
Else
    pdf.DateSaved = Nothing
End If

If Not IsDBNull(ds.Tables(0).Rows(i)("dateimported")) Then
    pdf.DateImported= Format(ds.Tables(0).Rows(i)("dateimported"), "yyyy-MM-dd")
Else
    pdf.DateImported= Nothing
End If

How can I do this to avoid doing so many If statements below. This way seems inefficient to me, can anyone suggest an better approach to initializing the object in scenario one or two? If the question is unclear, please do let me know, I will try and explain.

Please note this is sample data.


回答1:


From reading T Field<T>(this DataRow row, string columnName), I believe that there is a check for the DBNull.Value for both reference and value types, returning a default value if DBNull.Value is passed.

So you can use it instead of checking for DBNull.Value each time:

.FileName = ds.Tables(0).Rows(i).Field(Of String)("FileName")

If the value of the specified DataColumn is null and T is a reference type or nullable type, the return type will be null. The Field method will not return Value.

DataRowExtensions.Field

Since you cant use this then @TimSchmelter provided an answer which you could build upon:

.FileId = If(row.IsNull("fileid"), 0, Convert.ToInt32(row("fileid"))




回答2:


Its not inefficient but a lot of code, so maybe there is a more readable/maintainable approach.

The If operator is one, you can also use DataRow.IsNull:

For i As Int32 = 0 To ds.Tables(0).Rows.Count - 1
    Dim row = ds.Tables(0).Rows(i)
    Dim pdf = New Pdf With {
        .FileId = If(row.IsNull("fileid"), 0, row.Field(Of Int32)("fileid")),
        .AccountNumber = If(row.IsNull("accountnumber"), 0, row.Field(Of Int32)("accountnumber")),
        .DateSaved = If(row.IsNull("datesaved"), Date.MinValue, row.Field(Of Date)("datesaved")),
        .FileName = If(row.IsNull("filename"), "", row.Field(Of String)("filename")),
        .DateImported = If(row.IsNull("dateimported"), Date.MinValue, row.Field(Of Date)("dateimported"))
    }
Next

(changed the name of your class from Object to Pdf)

You should also set Option Strict to On, then you need to code type safe but you gain compile time safety and avoid unwanted conversions.



来源:https://stackoverflow.com/questions/32271839/object-initializing-with-db-null-check

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!