You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

更新JPG图片时出错:无法将Bitmap转换为byte[]

解决"failed to convert parameter value from bitmap to byte[]"数据库更新错误

Hey there, let's fix that image update error you're hitting! The root cause is pretty straightforward—you're trying to pass a Bitmap object directly to a SQL parameter that expects a byte array, which is why you're getting that conversion error.

问题出在这一行:

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

SQL Server's Image parameter type requires a byte array, not a raw Bitmap object. You actually already wrote the correct conversion code—you just commented it out! Let's get this sorted, plus fix a few other potential issues in your code.

修正后的完整代码:

private void btnupdate_Click(object sender, EventArgs e) 
{ 
    try 
    {
        // Use using statements to auto-manage resource cleanup (no more manual conn.Close()!)
        using (var conn = new SqlConnection("你的数据库连接字符串")) // Replace with your actual connection string
        {
            conn.Open(); 
            byte[] imageBytes = null;

            // Convert Bitmap to byte array only if there's an image present
            if (pb1.Image != null)
            {
                using (MemoryStream ms = new MemoryStream()) 
                {
                    pb1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
                    imageBytes = ms.ToArray(); 
                }
            }

            SqlCommand command = new SqlCommand(@"Update Stock_Jewelry 
                set Stock_Type = @Stock_Type, 
                    Stock_No = @Stock_No, 
                    Quantity = @Quantity, 
                    Item_Description = @Item_Description, 
                    Item_Type = @Item_Type, 
                    No_of_Gems = @No_of_Gems, 
                    Gem_Type = @Gem_Type, 
                    Image = @Image 
                WHERE ID = @ID", conn); 

            // Map parameters correctly, fix type conversions for integer fields
            command.Parameters.Add("@ID", SqlDbType.VarChar).Value = txt_ID.Text; 
            command.Parameters.Add("@Stock_Type", SqlDbType.VarChar).Value = Stock_Type.Text; 
            command.Parameters.Add("@Stock_No", SqlDbType.NVarChar).Value = txt_stock_no.Text;
            // Convert text input to integer (add validation if needed!)
            command.Parameters.Add("@Quantity", SqlDbType.Int).Value = int.Parse(txt_qty.Text); 
            command.Parameters.Add("@Item_Description", SqlDbType.NVarChar).Value = combo_itemk_description.Text; 
            command.Parameters.Add("@Item_Type", SqlDbType.NVarChar).Value = combo_item_type.Text; 
            command.Parameters.Add("@No_of_Gems", SqlDbType.Int).Value = int.Parse(txt_no_of_gems.Text); 
            command.Parameters.Add("@Gem_Type", SqlDbType.NVarChar).Value = txt_gem_type.Text; 
            command.Parameters.Add("@Image", SqlDbType.Image).Value = imageBytes; 

            command.ExecuteNonQuery();
            MessageBox.Show("You've updated successfully!", "Successful Message", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        } // Connection auto-closes here when using block ends
    } 
    catch (Exception ex) 
    { 
        MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
    this.Close(); 
}

额外优化建议:

  • Add input validation for integer fields (like Quantity, No_of_Gems) using int.TryParse to avoid format errors:
    int quantity;
    if (!int.TryParse(txt_qty.Text, out quantity))
    {
        MessageBox.Show("Please enter a valid number for Quantity!", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        return;
    }
    command.Parameters.Add("@Quantity", SqlDbType.Int).Value = quantity;
    
  • SQL Server's Image type is deprecated—switch to VarBinary(MAX) in your database schema for better compatibility with modern SQL Server versions.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:48:18