簡體   English   中英

如何修復此錯誤參數無效

[英]How fix this error Parameter is not valid

我在讀取SQL圖像時遇到問題,我的表是下一個結構

CREATE TABLE [dbo].[Nota] (
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Titulo] [varchar](50) NOT NULL,
    [Imagen] [image] NOT NULL
)

比我已經保存在數據庫中的字段是下一個

Id  Titulo          Imagen
1   Nombreimagen    0x53797374656D2E427974655B5D

我的代碼如下

try
{
   System.Data.DataSet ds = new System.Data.DataSet();
   System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM Nota WHERE ID = '1'", @"Data Source=Server;Initial Catalog=DB;Integrated Security=True;User ID=User;Password=Pasword*");
   da.Fill(ds, "Imagen");

       byte[] imageBuffer = (byte[])ds.Tables["Imagen"].Rows[0]["Imagen"];
       System.IO.MemoryStream ms = new System.IO.MemoryStream(imageBuffer);
       pictureBox2.Image = Image.FromStream(ms);         
}
catch (System.Exception ex)
{
   MessageBox.Show(ex.Message);
}

我的錯誤是以下

[System.ArgumentException] = {“參數無效。”}

在下一行

pictureBox2.Image = Image.FromStream(ms);

嘗試使用數據類型varbinary(max)代替圖像數據類型,因為不推薦使用圖像數據類型。

https://docs.microsoft.com/zh-cn/sql/t-sql/data-types/ntext-text-and-image-transact-sql

每當GDI無法從您提供的字節中生成圖像時,都會出現該錯誤。

讓我們看一下圖像字段中的內容:

0x53797374656D2E427974655B5D

如果我通過一個HexString到Byte轉換器,然后通過ASCII(如果您知道table的話,會有所幫助)對結果進行解碼,例如使用以下代碼:

Encoding.ASCII.GetString(  StringToByteArray("53797374656D2E427974655B5D")).Dump();

public static byte[] StringToByteArray(String hex)
{
  int NumberChars = hex.Length;
  byte[] bytes = new byte[NumberChars / 2];
  for (int i = 0; i < NumberChars; i += 2)
    bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
  return bytes;
}

結果是:

System.Byte []

那不是圖像。 嘗試插入或更新該行時,它是保存圖像的類型的ToString()方法的結果。 當您嘗試保存圖像時,可能要確保圖像的SqlParameter的DbType屬性設置為DbType.Binary

您無法在此處顯示的代碼中解決問題,無法解決根本原因,也不能解決行中Imagen字段的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM