Pregunta

Why can't I insert DBNull.Value into a nullable image-field in sql server 2005?

I tried this code:

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();

item is of type 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; }
}

When inserting I get this exception:

Operand type clash: nvarchar is incompatible with image

Why DBNull.Value has got problems only with image? Why not with datetime?

¿Fue útil?

Solución

You could try this:

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

Otros consejos

I think this could be to do with the fact that you are not explicitly defining the SqlParameter.SqlDbType and think it will be assuming NVARCHAR. Instead of using AddWithValue, trying adding a new SqlParameter and setting SqlDbType explicitly to SqlDbType.Image.

Here's the MSDN ref which states the default is NVARCHAR.

You need to explicitly specify the parameter's type as SqlDbType.Image

I solved this like this:

If (Photo.Equals(DBNull.Value)) Then
    cmd.Parameters.Add("Photo", SqlDbType.Image).Value = DBNull.Value
Else
    cmd.Parameters.AddWithValue("Photo", Photo)
End If
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top