2016-11-07 119 views
0

一个表包含一个名为“电话”我想插入空值由实体框架的工作此列BIGINT列。我用下面的代码:插入空值到SQL Server BIGINT列

objRegister.Phone = Convert.ToInt64(txtPhone.Text ??null ); 

但我得到这个消息: “Input string was not in a correct format.”。 我更改代码如下图所示:

objRegister.Phone = txtMobile.Text != null ?Convert.ToInt64(txtMobile.Text):((long?)null) ; 

我得到了同样的信息: “Input string was not in a correct format.

回答

1

你可以试试这个是这样的:

long phone; 
objRegister.Phone = long.TryParse(txtPhone.Text, out phone) 
        ? (long?)phone 
        : (long?)system.DBNullValue; 
1
Int64 phoneNumber; 
Int64.TryParse(txtPhone.Text, out phoneNumber) == true ? phoneNumber : DBNull.Value; 

您可以使用?: Conditional OperatorInt64.TryParse检查文本是否可以转换为Int64。如果转换然后分配给其他DBNull.Value

+0

转换价值这是行不通的 - :运营商需要在两个表达式真假同一类型 – Fabio