sql-server-2005 - 为什么我不能将 DBNull.Value 插入到 sql server 2005 中的可为 null 的图像字段中?

标签 sql-server-2005 c#-2.0 nullable dbnull

为什么我不能将 DBNull.Value 插入到 sql server 2005 中可为空的 image 字段中?

我试过这段代码:

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;

关于sql-server-2005 - 为什么我不能将 DBNull.Value 插入到 sql server 2005 中的可为 null 的图像字段中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8985374/

相关文章:

sql - T-SQL - 字符串连接

c# - 如何从远程 url 获取有效的文件名和扩展名以保存它。?

c# - 列表中的最大日期时间

.net - Nullable Enum 可空类型问题

sql - Sql Server事件监视器中的这个进程是什么?

c# - 使用sqlserver 2005在特定位置插入值

.net - ReadProcessMemory 的最快方法是什么?

vb.net - vb.net 中是否有可空的 bool 之类的东西

java - 使用 Java Checker Framework 的 Nullness Checker 注释可重入性

sql - SQL Server 中的 LIKE 运算符区分大小写吗?