簡體   English   中英

有沒有辦法將 Datatable 的 System.Data.SqlTypes.SqlBinary 轉換為 System.Byte[] 類型?

[英]Is There a Way to Convert System.Data.SqlTypes.SqlBinary of Datatable To Type System.Byte[]?

我在 C# 中有一個動態圖片框,它從一列數據表中獲取圖片,數據表結構是:

public DataTable Pictures = new DataTable();
Pictures.Columns.Add("id", typeof(bool));
Pictures.Columns.Add("filebytes", typeof(SqlBinary));

我有一個字節數組來在圖片框中顯示圖片:

byte[] ImageBinary;

以下代碼用於顯示圖像:

                        PictureBox a = new PictureBox();      
                        a.Location = new Point(100, 90);
                        a.Size = new Size(25, 25);
                        a.SizeMode = PictureBoxSizeMode.StretchImage;
                        groupBox3.Controls.Add(a);
                        MemoryStream ms = null;
                        Image img = null;
                        Tools b = new Tools();
                        ImageBinary = b.CreateImageBinary(openFileDialog1.FileName);
                        ms = new MemoryStream(ImageBinary);
                        Pictures.Rows.Add(new object[] { i, ImageBinary });
                        img = Image.FromStream(ms);
                        a.Image = img;
                        ms.Close();

它從 Openfiledialog 獲取圖像

我想從數據表中為圖片框分配一個值我的問題是當我像這樣更改代碼時:

 ImageBinary = (byte[])Pictures.Rows[sm][1]; //sm is a variable to specific a row of datatable

它讓我出現錯誤,例如:

無法將“system.data.sqltypes.sqlbinary”類型的對象轉換為“system.byte[]”類型

我很困惑請幫幫我

您可以使用DqlBinary.Explicit 運算符

public static explicit operator byte[] (System.Data.SqlTypes.SqlBinary x);

我的建議是添加using System.Data.SqlTypes; 在聲明顯式運算符的.cs文件的開頭導入。

檢查包含它的 dll 的文檔,以防編譯器找不到命名空間。

暫無
暫無

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

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