DBからデータを取得した時にnullかどうかを判断するのには、System.DBNull を使用する。こんな簡単なことがなかなか見つからなかったのでイライラ。。


using System;
using System.Data;
using System.Data.SqlClient;

class Class1
{
[STAThread]
static void Main(string[] args)
{
DataSet myDS = new DataSet();
string myConnString = "data source=server_name;initial catalog=db_name";

SqlDataAdapter myAdapter = new SqlDataAdapter("select * from table1", myConnString);
try
{
myAdapter.Fill(myDS, "table1");
foreach(DataRow myTitle in myDS.Tables["table1"].Rows)
{
object col1_data = myTitle["col1"];
if( col1_data is System.DBNull )
{
Console.WriteLine("dbnull");
continue;
}
}
}
catch (System.Data.SqlClient.SqlException e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}