为什么我无法插入 DBNull.Value 变成可为空的 image-sql server 2005 中的字段?

我尝试了这段代码:

SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=PrescriptionTrackingSystem;Integrated Security=True");

            conn.Open();

            SqlTransaction transaction = conn.BeginTransaction();

            SqlCommand command = new SqlCommand(@"INSERT INTO Customer(
                               ID
                              ,Name
                              ,TelephoneNumber
                              ,DateOfBirth,
                               InsuranceProvider,
                               PolicyNumber,Photo)
                          VALUES(
                               @ID
                              ,@Name
                              ,@TelephoneNumber
                              ,@DateOfBirth,
                               @InsuranceProvider,
                               @PolicyNumber,
                               @Photo)", conn);

            command.Transaction = transaction;

            command.Parameters.AddWithValue("@ID", 1000);
            command.Parameters.AddWithValue("@Name", item.Name);
            command.Parameters.AddWithValue("@TelephoneNumber", item.TelephoneNumber);
            if (item.DateOfBirth != null)
            {
                command.Parameters.AddWithValue("@DateOfBirth", item.DateOfBirth);
            }
            else
            {
                command.Parameters.AddWithValue("@DateOfBirth", DBNull.Value);
            }
            command.Parameters.AddWithValue("@InsuranceProvider", item.InsuranceProvider);
            command.Parameters.AddWithValue("@PolicyNumber", item.PolicyNumber);

            if (item.Photo != null)
            {
                command.Parameters.AddWithValue("@Photo", item.Photo);
            }
            else
            {
                command.Parameters.AddWithValue("@Photo", DBNull.Value);
            }

            int count = command.ExecuteNonQuery();

            transaction.Commit();

            conn.Close();

项目属于类型 Customer.

public class Customer
{        
    public string Name { get; set; }
    public DateTime? DateOfBirth { get; set; }
    public string TelephoneNumber { get; set; }
    public string InsuranceProvider { get; set; }
    public int? PolicyNumber { get; set; }
    public byte [] Photo { get; set; }
}

插入时我得到这个异常:

Operand type clash: nvarchar is incompatible with image

为什么 DBNull.Value 仅遇到问题 image?为什么不与 datetime?

有帮助吗?

解决方案

你可以试试这个:

command.Parameters.Add("@Photo", SqlDbType.Image).Value = DBNull.Value;

其他提示

我认为这可能与您没有显式定义 SqlParameter.SqlDbType 并且认为它将假设 NVARCHAR 有关。尝试添加新的 SqlParameter 并将 SqlDbType 显式设置为 SqlDbType.Image,而不是使用 AddWithValue。

这是 MSDN 参考 其中指出默认值是 NVARCHAR。

您需要显式指定参数的类型为 SqlDbType.Image

我这样解决了这个问题:

If (Photo.Equals(DBNull.Value)) Then
    cmd.Parameters.Add("Photo", SqlDbType.Image).Value = DBNull.Value
Else
    cmd.Parameters.AddWithValue("Photo", Photo)
End If
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top