3 回答

TA貢獻(xiàn)1818條經(jīng)驗 獲得超7個贊
錯誤原因:在面向?qū)ο蟮木幊陶Z言中,null表示缺少對對象的引用。DBNull表示未初始化的變體或不存在的數(shù)據(jù)庫列。來源:MSDN
我遇到的實際代碼錯誤:
在更改代碼之前:
if( ds.Tables[0].Rows[0][0] == null ) // Which is not working
{
seqno = 1;
}
else
{
seqno = Convert.ToInt16(ds.Tables[0].Rows[0][0]) + 1;
}
更改代碼后:
if( ds.Tables[0].Rows[0][0] == DBNull.Value ) //which is working properly
{
seqno = 1;
}
else
{
seqno = Convert.ToInt16(ds.Tables[0].Rows[0][0]) + 1;
}
結(jié)論:當(dāng)數(shù)據(jù)庫值返回null值時,我們建議使用DBNull類,而不是像C#語言一樣指定為null。
- 3 回答
- 0 關(guān)注
- 1176 瀏覽
添加回答
舉報