programing

VB의 null 가능한 유형입니다.NET?

golfzon 2023. 5. 9. 23:27
반응형

VB의 null 가능한 유형입니다.NET?

VB에서 Null 가능한 유형을 사용할 수 있습니다.NET? 그렇다면 SQL Server에서 NULL을 허용하는 필드와 함께 사용할 수 있는 Nullable Integer가 가능합니까?예를 들어주시면 감사하겠습니다.

VB.Net에는 null 형식이 있으며 다음 두 가지 방법으로 선언할 수 있습니다.

Dim iNullable As Integer?

또는

Dim iNullable As Nullable(Of Integer)

VB에는 null 형식을 사용할 수 있습니다.NET은 C#과 동일합니다.

할당할 수 없습니다.Null,Nothing또는DBNull맨몸으로IntegerVB에서 대신 를 사용할 수 있습니다.

Dim x As Integer? = Nothing

또는 때때로 (거의) 다음과 같은 경우에 정수 값을 상자에 넣는 것이 타당합니다.Object(될 수 있음)Nothing).

정수(System.Int32등)에서.NET은 직접 null로 설정할 수 없지만 값 유형을 null로 설정할 수 있습니다.데이터베이스를 확인해야 할 수도 있습니다.DBNull보다는null/Nothing.

네, 아주 비슷한 것을 할 수 있습니다.

많은 경우에Nothing기본값으로 변환됩니다.사용하기Nothing당신이 사용하는 것과 같은 방법.null올바른 null 가능 유형으로 캐스팅해야 합니다.

Dim str As String
Dim int As Nullable(Of Integer) ' or use As Integer?
Dim reader As SqlDataReader
Dim colA As Integer = reader.GetOrdinal("colA")
Dim colB As Integer = reader.GetOrdinal("colB")
str = If(reader.IsDBNull(colA), DirectCast(Nothing, String), reader.GetString(colA))
int = If(reader.IsDBNull(colB), DirectCast(Nothing, Nullable(Of Integer)), reader.GetInt32(colB))

클라이언트 코드에서 NULL을 DB에 입력하는 방법은 (아래 질문에서 질문한 바와 같이) SQL의 기본값을 NULL과 동일하게 사용하는 방법밖에 없습니다.

따라서 저장 프로시저 업데이트/삽입에서 다음과 같은 방법을 사용할 수 있습니다.

create proc dbo.UpdateSomeTable
    @Id int, 
    @Status bit = NULL
as
begin
    update dbo.SomeTable
    set Status = @Status
    where Id = @Id
end

그리고 코드 어딘가에.

Dim pars As List(Of SqlParameter) = New List(Of SqlParameter)
With pars
    .Add(New SqlParameter("@Id", LogbookNoteId))
    If Flag Then
        .Add(New SqlParameter("@Status", Flag))
    End If
End With
ExecuteNonQuery("dbo.UpdateSomeTable", pars, CommandType.StoredProcedure)

아닙니다. 데이터베이스에서 null을 가져오려면 해당 값을 추가(또는 업데이트)하지 않도록 삽입 또는 업데이트 쿼리를 수정해야 합니다.

언급URL : https://stackoverflow.com/questions/1123844/nullable-types-in-vb-net

반응형