Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 没有图像就不能保存_C# - Fatal编程技术网

C# 没有图像就不能保存

C# 没有图像就不能保存,c#,C#,如果有必要,我需要在没有图像的情况下保存此记录 这是我的代码,我也使用了if语句: private void btnsave_Click(object sender, EventArgs e) { try { string commandText = "INSERT INTO Stock_Jewelry VALUES(@Stock_Type,@stock_no,@Quantity,@image)"; Sql

如果有必要,我需要在没有图像的情况下保存此记录

这是我的代码,我也使用了if语句:

private void btnsave_Click(object sender, EventArgs e)
    {
        try
        {
            string commandText = "INSERT INTO Stock_Jewelry VALUES(@Stock_Type,@stock_no,@Quantity,@image)";

            SqlCommand command = new SqlCommand(commandText, conn);

            command.Parameters.Add("@Stock_Type", SqlDbType.VarChar);
            command.Parameters["@Stock_Type"].Value = Stock_Type.Text;

            command.Parameters.Add("@stock_no", SqlDbType.NVarChar);
            command.Parameters["@stock_no"].Value = txt_stock_no.Text;

            command.Parameters.Add("@Quantity", SqlDbType.Int);
            command.Parameters["@Quantity"].Value = txt_qty.Text;

            if(pb1 != null) { 
            MemoryStream stream = new MemoryStream();
            pb1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] pic = stream.ToArray();
            command.Parameters.AddWithValue("@image", pic);
            }
            else {
                pb1 = null;
            }

            command.ExecuteNonQuery();
            MessageBox.Show("You've inserted successfully!", "Successful Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Hide();

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }
根据评论编辑:


如果保存时没有图像,则可以使用图像进行保存,它会显示:对象引用未设置为对象的实例


如果图像列允许空值,请更改为

if(pb1 != null) 
{ 
    MemoryStream stream = new MemoryStream();
    pb1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
    byte[] pic = stream.ToArray();
    command.Parameters.AddWithValue("@image", pic);
}
else 
{
    command.Parameters.AddWithValue("@image", System.Data.SqlTypes.SqlBinary.Null); 
    // edit: replaced incorreect DBNull.Value due to comment by Heinzi
}

如果pb1为空,则当前您没有为Sql提供足够的参数。构造Sql时,它尝试访问附加到SqlCommand实例的SqlParameterCollection。未找到SqlCommand中给定的@image的任何内容-因此出现错误

允许数据库中的图像列为NULL。。。。并为图像添加一个NULL作为参数,您需要使图像列可为NULL,以使其正常工作。这不是一个非常有用的错误描述。请粘贴你的问题中的错误。如果我保存时没有图像,我可以使用图像保存它说:对象引用未设置为objectcheck conn的实例为null-这是我在这段代码中看到的唯一可能为null的内容,如果图像列是varbinary列,它应该为,您不能将AddWithValue与DBNull.Value一起使用,请参阅我尝试了代码,因为答案仍然无效。我的表列是:Image.sir我的不是varbinary,而是Image。您认为varbinary比image好吗?@mmsms列映像的数据库类型是字节数组。您的代码获取Imagetype并将其数据流保存到字节[]中,然后将该字节作为参数“@image”附加到SQL中进行存储。请参阅-图像只是一个字节[]