“system.dbnull”错误导致无效投射

时间:2015-04-23 16:50:34

标签: c# sql-server wpf

我从以下代码获取更新中的system.dbnull错误。某些值在数据库中为null,并且在更新时将返回null。我相信我应该将默认值设置为null,因此它将停止返回dbnull错误,但我不确定如何/在何处执行此操作。

const string sql =
    @"UPDATE DoctorFacility SET PVId = @PVId, 
                                Type = @Type,
                                Inactive = @Inactive, 
                                Prefix = @Prefix,
                                First = @First,
                                Middle = @Middle,
                                Last = @Last,
                                Suffix = @Suffix,
                                OrgName = @OrgName,
                                OrgType = @OrgType, 
                                ListName = @ListName, 
                                Address1 = @Address1, 
                                Address2 = @Address2, 
                                City = @City, 
                                State = @State, 
                                Zip = @Zip, 
                                Country = @Country, 
                                Phone1 = @Phone1, 
                                Phone1Type = @Phone1Type, 
                                Phone2 = @Phone2, 
                                Phone2Type = @Phone2Type, 
                                EmailAddress = @EmailAddress, 
                                PlaceOfServiceMId = @PlaceOfSErviceMId, 
                                UPIN = @Upin, 
                                SpecialtyMId = @SpecialtyMId, 
                                DotId = @DotId, 
                                NPI = @NPI, 
                                CreatedBy = @CreatedBy, 
                                LastModifiedBy = @LastModifiedBy 
        WHERE DotId = @DotId";
 using (SqlDataAccessor dataAccessor = DataAccessorFactory.GetDataAccessor(Connection))
 {
    string user = UserNameFactory.GetUsername();
    DaParameters parameters = new DaParameters(dataAccessor);
    parameters.Add("@PVId", DateTimeHash(DateTime.Now));
    parameters.Add("@Type", (int)provider.ProviderType);
    parameters.Add("@Inactive", provider.Inactive);
    parameters.Add("@Prefix", provider.Prefix, DbType.AnsiString, 10);
    parameters.Add("@First", provider.FirstName, DbType.AnsiString, 35);
    parameters.Add("@Middle", provider.MiddleName, DbType.AnsiString, 30);
    parameters.Add("@Last", provider.LastName, DbType.AnsiString, 60);
    parameters.Add("@Suffix", provider.Suffix, DbType.AnsiString, 20);
    parameters.Add("@OrgName", provider.OrgName, DbType.AnsiString, 60);
    parameters.Add("@OrgType", provider.OrgType, DbType.AnsiString, 3);
    parameters.Add("@ListName", provider.ListName, DbType.AnsiString, 160);
    parameters.Add("@Address1", provider.Address1, DbType.AnsiString, 50);
    parameters.Add("@Address2", provider.Address2, DbType.AnsiString, 50);
    parameters.Add("@City", provider.City, DbType.AnsiString, 30);
    parameters.Add("@State", provider.State, DbType.AnsiString, 3);
    parameters.Add("@Zip", provider.Zip, DbType.AnsiString, 10);
    parameters.Add("@Country", provider.Country, DbType.AnsiString, 30);
    parameters.Add("@Phone1", provider.Phone1, DbType.AnsiString, 15);
    parameters.Add("@Phone1Type", provider.Phone1Type, DbType.AnsiString, 25);
    parameters.Add("@Phone2", provider.Phone2, DbType.AnsiString, 15);
    parameters.Add("@Phone2Type", provider.Phone2Type, DbType.AnsiString, 25);
    parameters.Add("@EmailAddress", provider.EmailAddress, DbType.AnsiString, 255);
    parameters.Add("@PlaceOfServiceMId", provider.PlaceOfServiceMID);
    parameters.Add("@UPIN", provider.UPIN, DbType.AnsiString, 50);
    parameters.Add("@SpecialtyMId", provider.SpecialtyMID);
    parameters.Add("@DotId", provider.DotID, DbType.AnsiString, 15);
    parameters.Add("@NPI", provider.NPI, DbType.AnsiString, 80);
    parameters.Add("@CreatedBy", user, DbType.AnsiString);
    parameters.Add("@LastModifiedBy", user, DbType.AnsiString);
    return dataAccessor.ExecuteIdentityInsert<Provider>(sql, parameters);
 }

}

1 个答案:

答案 0 :(得分:1)

如果它在ADO.NET上运行,它不知道如何将.NET空值转换为SQL Server空值。

尝试将任何可疑的空输入空值合并到System命名空间中的DBNull.Value,如下所示:

parameters.Add("@First", provider.FirstName ?? DBNull.Value, DbType.AnsiString, 35);