You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

WPF中ImageBox.Source为null时无法将数据保存到SQLite的问题

解决SQLite保存无照片数据时的无效操作错误

这个问题的核心在于当没有照片时,你传入了未初始化或null的BitmapSource对象到转换方法,同时SQL参数没有正确处理空值,下面是分步解决方案:

1. 修复BitmapSourceToByteArray方法的null处理

当传入的image为null时,直接返回null(或者空字节数组,根据你的数据库字段规则选择),避免调用BitmapFrame.Create(null)引发无效操作错误:

public byte[] BitmapSourceToByteArray(BitmapSource image) 
{ 
    if (image == null)
    {
        return null; // 若数据库字段要求非空,可改为return new byte[0];
    }
    using (var stream = new MemoryStream()) 
    { 
        var encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image)); 
        encoder.Save(stream); 
        return stream.ToArray(); 
    } 
}

2. 在DataSave方法中正确处理空照片的SQL参数

SQLite参数不能直接传入null,需要用DBNull.Value来表示数据库中的空值,修改照片参数的赋值逻辑:

public bool DataSave(string TableName, CustomerInfo CInfox, BitmapImage obj) 
{ 
    ConOpen(); 
    string query = $"Insert Into {TableName} (Company, Product, Product_No, Product_Size, Photo) Values (@company, @product, @pno, @psize, @photo)"; 
    SQLiteCommand komut = new SQLiteCommand(query, connection); 
    // 其他参数定义保持不变
    SQLiteParameter param1 = new SQLiteParameter("@company", DbType.String); 
    SQLiteParameter param2 = new SQLiteParameter("@product", DbType.String); 
    SQLiteParameter param3 = new SQLiteParameter("@pno", DbType.String); 
    SQLiteParameter param4 = new SQLiteParameter("@psize", DbType.String); 
    
    // 处理照片参数的空值情况
    byte[] photoBytes = BitmapSourceToByteArray(obj);
    SQLiteParameter param5 = new SQLiteParameter("@photo", DbType.Binary); 
    param5.Value = photoBytes == null ? DBNull.Value : (object)photoBytes; 
    
    param1.Value = CInfox.Customer; 
    param2.Value = CInfox.Product; 
    param3.Value = CInfox.ProductNo; 
    param4.Value = CInfox.ProductSize; 
    
    komut.Parameters.Add(param1); 
    komut.Parameters.Add(param2); 
    komut.Parameters.Add(param3); 
    komut.Parameters.Add(param4); 
    komut.Parameters.Add(param5); 
    
    komut.ExecuteNonQuery(); 
    MessageBox.Show("Saved Succesfully!"); 
    ConClose(); 
    return true; 
}

3. 修正SaveButton_OnClick中的照片对象传递逻辑

当前代码中,即使ImageBox.Source为null,你还是创建了一个空的BitmapImage并传入方法,这会导致转换方法处理未初始化的对象。修改为直接传递null:

private void SaveButton_OnClick(object sender, RoutedEventArgs e) 
{ 
    BitmapImage image = null; // 初始化为null
    if (ImageBox.Source != null) 
    { 
        image = new BitmapImage();
        image.BeginInit(); 
        image.UriSource = new Uri(filephoto.FileName); 
        image.EndInit(); 
        ImageBox.Source = image; 
    } 
    helper.DataSave("CompanyData", CInfo, image); // 无照片时传入null
}

额外建议:添加异常处理

数据库操作容易出现意外,恢复try-catch并添加finally确保连接关闭:

public bool DataSave(string TableName, CustomerInfo CInfox, BitmapImage obj) 
{ 
    try
    {
        ConOpen(); 
        // 现有逻辑代码...
        return true; 
    }
    catch (Exception ex)
    {
        MessageBox.Show($"保存失败:{ex.Message}");
        return false;
    }
    finally
    {
        ConClose(); // 无论是否异常,确保连接关闭
    }
}

这样修改后,无论是否有照片,都能正常保存数据到SQLite了。

内容的提问来源于stack exchange,提问作者user16741817

火山引擎 最新活动