以下に示すように、Access 2007 に対応するテーブルを持つクラスがあります。
Class Product
{
public int ProductId
{
get;set;
}
public string ProductName
{
get;set;
}
public string ProductColor
{
get;set;
}
}
これは、製品をデータベースに追加するために使用する方法です。
public static void AddProduct(Product product)
{
con.Open();
string strSql = @"INSERT INTO PRODUCTS (PRODUCTID,PRODUCTNAME,PRODUCTCOLOR)
VALUES(?,?,?)";
IDbCommand cmd = con.CreateCommand();
cmd.CommandText = strSql;
dh.AddInParam(cmd, "@productId", DbType.Int32, product.ProductId);
dh.AddInParam(cmd, "@productName", DbType.String, product.ProductName);
dh.AddInParam(cmd, "@productColor", DbType.String, product.ProductColor);
cmd.ExecuteNonQuery();
con.Close()
}
これで、ユーザーが null 値を入力してもかまいません。その場合、どうすれば AddProduct メソッドを更新できますか? 私の頭に浮かぶ唯一のケースは、以下のように各パラメーターに対してnullチェックを行うことです..
if(product.ProductColor = null)
{
dh.AddInParam(cmd, "@productColor", DbType.String, DbNull.Value);
}
else
{
dh.AddInParam(cmd, "@ProductColor, DbType.String, product.ProductColor)
}
明らかな何かが欠けていますか?これらのパラメーター化されたクエリで null をチェックするための最良の方法は何ですか?