Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

vb.net - Object initializing with db null check

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

Example Dataset

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...