错误:无法将参数值从SqlBinary转换为Byte []

时间:2017-08-30 05:46:13

标签: c# ms-access

public class MyClass 
{
    public int ID { get; set; }
    public string Name { get; set; }
    public byte[] Photo { get; set; }
}    

public class Program
{
    static void Main(string[] args)
    {
        string imageFileName = @"D:\Demure.jpg";
        string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\db1.mdb;Persist Security Info=False";
        string providerName = @"System.Data.OleDb";            
        string SQL_UPDATE = @"update  Word SET Name=@Name, Photo=@Photo where ID=@ID"; 

        DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);

        IDbConnection Connection = factory.CreateConnection();
        Connection.ConnectionString = connString;
        Connection.Open();

        IDbTransaction Transaction = Connection.BeginTransaction();

        IDbCommand Command = factory.CreateCommand();
        Command.Connection = Connection;
        Command.Transaction = Transaction;

        Command.CommandText = SQL_UPDATE;            

        IDbDataParameter param2 = Command.CreateParameter();
        param2.ParameterName = "Name";
        param2.Value = "Hello?";
        param2.DbType = DbType.String;
        Command.Parameters.Add(param2);

        IDbDataParameter param3 = Command.CreateParameter();
        param3.ParameterName = "Photo";
        param3.Value = SqlBinary.Null;//ConvertImage.ToByteArray(Bitmap.FromFile(imageFileName));
        param3.DbType = DbType.Binary;
        Command.Parameters.Add(param3);

        IDbDataParameter param1 = Command.CreateParameter();
        param1.ParameterName = "ID";
        param1.Value = 1000;
        param1.DbType = DbType.Int32;
        Command.Parameters.Add(param1);

        IDataReader dataReader = Command.ExecuteReader();            

        // read data
        IList<MyClass> list = null;
        MyClass item = null;
        while (dataReader.Read())
        {
            if (list == null)
            {
                list = new List<MyClass>();
            }

            item = new MyClass();
            item.ID = dataReader.GetInt32(0);
            item.Name = dataReader.GetString(1);

            list.Add(item);
        }

        dataReader.Close();

        Transaction.Commit();

        string str = string.Empty;
    }
}

public static class ConvertImage
{
    public static byte[] ToByteArray(string imageLocation)
    {
        byte[] bytes = null;

        try
        {
            bytes = File.ReadAllBytes(imageLocation);
        }
        catch
        {
        }

        return bytes;
    }

    public static byte[] ToByteArray(System.Drawing.Image imageIn)
    {
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

        return ms.ToArray();
    }

    public static Image ToImage(string imageLocation)
    {
        byte[] bytes = ToByteArray(imageLocation);

        return ToImage(bytes);
    }

    public static Image ToImage(byte[] byteArrayIn)
    {
        Image returnImage = null;

        try
        {
            if (byteArrayIn != null)
            {
                MemoryStream ms = new MemoryStream(byteArrayIn);
                returnImage = Image.FromStream(ms);
            }
        }
        catch (Exception ex)
        {
            string str = String.Empty;
        }

        return returnImage;
    }
}

此程序在IDataReader dataReader = Command.ExecuteReader();

时出错

错误讯息:

An unhandled exception of type 'System.InvalidCastException' occurred in System.Data.dll

Additional information: Failed to convert parameter value from a SqlBinary to a Byte[].

我也试过SqlBytes.Null。给出相同的信息。

我该如何解决这个问题?

P.S。该字段已包含图像数据。我正在尝试使用Null值更新它。

1 个答案:

答案 0 :(得分:1)

相当于param3.Value = SqlBinary.Null;应为param3.Value = DBNull.Value;