2017-10-16 99 views
-1

當我將圖像插入SQL Server數據庫時,如果圖像爲空,則會將0x值插入到數據庫圖像列中。將圖像保存到SQL Server數據庫中

當我取回以0x十六進制值的圖像,我得到一個錯誤:

Parameter is not valid

string strsql = "SELECT [ID],PIC_Name FROM [HRMS].[dbo].[Employee_PC] where Id = '" + mFieldValue1 + "'"; 
myconn = new OleDbConnection(clsConnections.conString); 
myconn.Open(); 

cmd = new OleDbCommand(strsql, myconn); 
OleDbDataReader dr = null; 

dr = cmd.ExecuteReader(); 
while (dr.Read()) 
{ 
    if ((byte[])dr[1] == null) 
    //if (picData == null) 
    { 
     pictureBox1.Image = null; 
    } 
    else 
    { 
     MemoryStream ms = new MemoryStream((byte[])dr[1]); 
     ms.Position = 0; 
     pictureBox1.Image = new Bitmap(ms); 
    } 
} 
+0

檢查,如果從數據庫返回的值不是0X(或爲DBNull) –

+0

*您可以保存*圖像您的數據庫,但我強烈建議你只保存到實際文件的引用。 – Robert

回答

0

dr[1]不會null,所以都不會對(byte[])dr[1]。你想檢查那個數組中的第一個字節

byte[] picData = (byte[])dr[1]; 

if (picData[0] == 0) 
{ 
    pictureBox1.Image = null; 
} 
else 
{ 
    MemoryStream ms = new MemoryStream(picData); 
    ms.Position = 0; 
    pictureBox1.Image = new Bitmap(ms); 
} 
相關問題