日期:2014-05-20  浏览次数:20577 次

DBNull問題,請教"大家"!
string   filename= " ";  
   
if(myFile.PostedFile.ContentLength> 0)
{
    string   NM=myFile.PostedFile.FileName;
    int   i=NM.LastIndexOf( ". ");
    string   extNM=NM.Substring(i);
    filename= "QC "+mid+extNM;
    UploadFile(filename);
}
else
{
    filename=attachmentLb.Text;//這個
}

......

SqlParameter   YeWuAttachment=new   SqlParameter( "@YeWuAttachment ",SqlDbType.VarChar);
YeWuAttachment.Value=(filename==string.Empty?DBNull.Value:filename);
......

為什麼上面代碼會出錯,怎樣修正?謝謝大家!

錯誤:
CSSModify.cs(233,56):   error   CS0173:   Type   of   conditional   expression   cannot   be   determined   because   there   is   no   implicit   conversion   between   'System.DBNull '   and   'string '


------解决方案--------------------
YeWuAttachment.Value=(filename==string.Empty?DBNull.Value:filename); ==> YeWuAttachment.Value=(filename==string.Empty?DBNull.Value:(object)filename);
------解决方案--------------------
?:运算符的后两个值的类型必须相同
------解决方案--------------------
分開來是可以的。 但就簡化了那樣寫就是不行,搞不明白! 因此才有上面一問! 那是什麼原因呢? ============== 1。 ? A:B. A和B的类型必须一致。 我开始把 B的类型改成了object ,这句通过了, 但与 参数的Varchar类型又不一致了。 所以还是分开写。