我有一个名为 Person 的类(我们称之为实体),它映射到数据库表(Person 表)。此表的记录可能在所有字段中都包含空值,但只有一个,即 PK。
简而言之,我们如何定义 Person 实体:
...
private System.DateTime _BirthDate;
[DisplayName("Birth Date")]
[Category("Column")]
public System.DateTime BirthDate
{
get
{
try { return _BirthDate; }
catch (System.Exception err)
{ throw new Exception("Error getting BirthDate", err); }
}
set
{
try { _BirthDate = value; }
catch (System.Exception err)
{ throw new Exception("Error setting BirthDate", err); }
}
}
...
问题是当我得到人员名单时
var dsPersons = DataHelper.personTabelAdapter.GetFilteredPersons(nationalNo, name);
那些在 db 中为 null 的字段有异常,所以我无法将它们与 null 甚至 DBNull.Value 进行比较,请在调试中查看我的代码的下图
我想检查它们是否为空,以便我可以决定填充我的新对象字段或不填充它们。
这是我得到的异常的详细信息
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Data.StrongTypingException: The value for column 'BirthDateFa' in table 'Person' is DBNull. ---> System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.
at EcomService.Data.EcomDS.PersonRow.get_BirthDateFa() in c:\Projects\Project\EcomForms\EcomService\EcomService\Data\EcomDS.Designer.cs:line 30607
--- End of inner exception stack trace ---
at EcomService.Data.EcomDS.PersonRow.get_BirthDateFa() in c:\Projects\Project\EcomForms\EcomService\EcomService\Data\EcomDS.Designer.cs:line 30610
at EcomService.Classes.Entities.Person.GetFilteredPersons(String nationalNo, String name) in c:\Projects\Project\EcomForms\EcomService\EcomService\Classes\Entities\Person.cs:line 898
at EcomService.EService.GetFilteredPersons(String nationalNo, String name) in c:\Projects\Project\EcomForms\EcomService\EcomService\EService.asmx.cs:line 172
--- End of inner exception stack trace ---
这是我的网络方法
[WebMethod]
public List<Person> GetFilteredPersons(string nationalNo, string name)
{
return Person.GetFilteredPersons(nationalNo, name);
}
调用这个方法
static public List<Person> GetFilteredPersons(string nationalNo, string name)
{
List<Person> persons = new List<Person>();
var dsPersons = DataHelper.personTabelAdapter.GetFilteredPersons(nationalNo, name);
foreach (var dsPerson in dsPersons)
{
Person person = new Person();
person.BirthDateFa = dsPerson.BirthDateFa;
//I don't know how to deal with the exceptions here
.... //other fields
persons.Add(person);
}
return persons;
}
我真的被困在这件作品中了。欢迎您的回答。