在Sql服务器bigInt列中插入null值

本文关键字:插入 null Sql 服务器 bigInt | 更新日期: 2023-09-27 18:16:48

一个表包含名为"Phone"的bigInt列。我想通过实体框架向该列插入null值。我使用了以下代码:

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.">

在Sql服务器bigInt列中插入null值

您可以这样尝试:

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

您可以使用?:带Int64.TryParse的条件运算符检查文本是否可转换为Int64。如果可转换,则分配可转换值else DBNull.Value