参数在内存流中无效

时间:2013-09-12 13:27:33

标签: c# stream

运行此代码时,我收到“参数无效”异常。我正在尝试显示来自数据库的图像:

SqlConnection con = new SqlConnection("data source=.;Initial catalog=RMSDB;user=sa;password=....;");
con.Open();
string sql = String.Format("Select Emp_Pic_ImageData From Employees where Emp_Id='{0}'", TxtBoxId.Text);
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
    byte[] img = (byte[])(reader[0]);
    if (img == null)
    {
        PicboxEmployee.Image = null;
    }
    else
    {
        MemoryStream mstrm = new MemoryStream(img);
        PicBoxName.Image = Image.FromStream(mstrm); //error: "Parameter is not valid"
    }
}
else
{
    MessageBox.Show("this not exists");
}

4 个答案:

答案 0 :(得分:1)

发生'参数无效'错误,因为您从数据库中取回的字节数组不代表有效的图像二进制数据。

这种情况可能有多种原因。首先,看一下数据库中的数据 - 它可能不会存储您期望的二进制数据。

与此同时,我迅速将一个小型控制台应用程序整合在一起,完成了您所追求的目标:

class LoadImageFromDbSpike
{
    private const string ConnectionString = @"Data Source=[SERVERNAME];Initial Catalog=[DBNAME];Trusted_Connection=true;Connect Timeout=180;";
    private const string ImageOnDiskPath = @"c:\test.png";
    private const string OutputPath = @"c:\output.png";

    static void Main(string[] args)
    {
        var imageData = File.ReadAllBytes(ImageOnDiskPath);

        var imageId = StoreImageData(imageData);

        var imageDataFromDb = LoadImageData(imageId);
        File.WriteAllBytes(OutputPath, imageDataFromDb);
    }


    private static int StoreImageData(IEnumerable<byte> imageData)
    {
        const string insertCommand = "INSERT INTO ImageSpike (Image) " +
                                     "VALUES (@ImageData); " +
                                     "SELECT SCOPE_IDENTITY();";

        using (var con = new SqlConnection(ConnectionString))
        using (var cmd = new SqlCommand(insertCommand, con))
        {

            cmd.Parameters.AddWithValue("@ImageData", imageData);
            cmd.Connection.Open();

            return (int) (decimal) cmd.ExecuteScalar();
        }
    }

    private static byte[] LoadImageData(int id)
    {
        const string loadImageCommand = "SELECT TOP 1 Image FROM ImageSpike " +
                                        "WHERE Id = @Id; ";

        using (var con = new SqlConnection(ConnectionString))
        using (var cmd = new SqlCommand(loadImageCommand, con))
        {
            cmd.Parameters.AddWithValue("@Id", id);
            cmd.Connection.Open();

            var result = cmd.ExecuteScalar();
            return (byte[]) result;
        }
    }
}

答案 1 :(得分:0)

//code for saving image into sql server 2008 r2//////
            byte[] img = null;
            FileStream fs = new FileStream(imgLoc,FileMode.Open,FileAccess.Read);
            BinaryReader br=new BinaryReader(fs);
            img = br.ReadBytes((int)fs.Length);


            string conn = "data source=.;Initial catalog=RMSDB;user=sa;password=ibs;";
            SqlConnection con = new SqlConnection(conn);
            con.Open();
            string cmdd = String.Format("INSERT INTO Employees (Emp_Pic_ImageData)  VALUES('{0}')",  @img);

            SqlCommand cmd = new SqlCommand(cmdd, con);

            cmd.Parameters.Add(new SqlParameter("@img",img));
            int tempss = cmd.ExecuteNonQuery();

答案 2 :(得分:0)

//code for retrival of image from sql server////////////


            string sql = String.Format("Select Emp_Pic_ImageData From Employees where Emp_Id='{0}'", TxtBoxId.Text);
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader reader = cmd.ExecuteReader();
            reader.Read();
            if (reader.HasRows)
            {
                byte[] img = (byte[])(reader[0]);
                if (img == null)
                {
                    PicboxEmployee.Image = null;
                }
                else
                {
                    MemoryStream mstrm = new MemoryStream(img);
                    PicboxEmployee.Image = new System.Drawing.Bitmap(mstrm); //there is error of parameter is not valid.

                }
            }
            else
            {

                MessageBox.Show("this not exists");

            }

答案 3 :(得分:-1)

问题是因为,您从数据库中错误地引入了它。尝试更改您的代码:

while (registry.Read())

{                
                byte[] image = (byte[])registry["Image"];    

}