0

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

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

4 回答 4

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;
        }
    }
}
于 2013-09-12T14:27:45.063 回答
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();
于 2013-09-13T05:43:06.330 回答
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");

            }
于 2013-09-13T05:49:56.260 回答
-1

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

while (registry.Read())

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

}
于 2017-07-03T18:56:57.083 回答