[C# 문법] C# DataTable 에서 저장된 값들 Null 체크하는 방법


안녕하세요.

 

오늘은 C# DataTable에서 저장된 값들 중 Null값이 있는지 없는지 체크하는 방법에 대해서 알려드리려고 합니다.

 

다른 부연설명 없이 바로 예제 코드를 통해서 설명 드리도록 하겠습니다.


 

DataTable Null 체크 예제코드


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace test123

{

    class Program

    {

        static void Main(string[] args)

        {

            //  DataTable dt = new DataTable("Branches");  

            DataTable dt = new DataTable("Student");

            dt.Columns.Add("StudentId"typeof(Int32));

            dt.Columns.Add("StudentName"typeof(string));

            dt.Columns.Add("Address"typeof(string));

            dt.Columns.Add("MobileNo"typeof(string));

 

            //Data  

            dt.Rows.Add(1"Manish"null"0000000000");

            dt.Rows.Add(2"Venkat"null"111111111");

            dt.Rows.Add(3"Namit""Pune""1222222222");

            dt.Rows.Add(4"Abhinav""Bhagalpur""3333333333");

 

            //DataTalbe Null 체크

            for(int row = 0; row < dt.Rows.Count; row++)

            {

                if (DBNull.Value.Equals(dt.Rows[row]["Address"]))

                {

                    Console.WriteLine("Address Null 학생 이름 : {0}"

                        dt.Rows[row]["StudentName"].ToString());

                }

                else

                {

                    Console.WriteLine("Address Null 아닌 학생 이름 : {0}"

                        dt.Rows[row]["StudentName"].ToString());

                }

 

            }

        }

    }

}

 

Colored by Color Scripter

cs

 

실행 결과



위와 같이 현재 Addressnull값이 들어 있는 학생은 “Manish”, “Venkat” 두 명이고, Address 값이 들어 있는 학생은 “Namit”, “Abhinav” 이렇게 제대로 Null 유무가 체크돼서 출력되고 있는 것을 확인하실 수 있습니다.

 

감사합니다.^^


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY