2017-09-26 107 views
0

大家好我正在製作一個簡單的Web應用程序,它利用用戶的網絡攝像頭和/或允許您上傳圖片。請忽略圖像中的錯誤,這是故意完成的。將圖像字節存儲到數據庫不起作用

我獲得圖像的字節是這樣的:

enter image description here

現在我想它進入我的SQL數據類型的圖像數據庫。 在我的數據訪問層,我有這樣的參數:

enter image description here

public int AddParam(string ParamName, byte[] pic) 
    { 
     return AddParam(ParamName, pic); 

    } 

我進入了他們,並試圖像這樣執行它:

enter image description here

DataAccessLayer DataAccessLayer = new DataAccessLayer()  
DataAccessLayer.PictureCaptured(dlStudentID.SelectedValue.ToString(),imgByte.ToArray()); 
           DataSet ds = new DataSet(); 
           DataAccessLayer .WebExecute(out ds); 

它給了我這樣的錯誤:enter image description here

我不明白爲什麼它正在這樣做。

如果您有任何提示或解答,請讓我知道。

非常感謝。

+1

不要發佈代碼圖片,包含實際代碼並突出顯示,然後按下【{}】按鈕進行格式化。另外,你期望函數AddParam做什麼?你認爲這是什麼功能,並顯示該功能。 –

+0

如果這是** SQL Server **:將在未來版本的SQL Server中刪除'image'數據類型。避免在新的開發工作中使用這些數據類型,並計劃修改當前使用它的應用程序。改用'varbinary(max)'。 [見詳細](http://msdn.microsoft.com/en-us/library/ms187993.aspx) –

回答

0

使用此方法將圖像轉換成字節,

public byte[] ImageToByte(string imageLocation) 
{ 
    byte[] imageData = null; 
    FileInfo fileInfo = new FileInfo(imageLocation); 
    long imageFileLength = fileInfo.Length; 
    FileStream fs = new FileStream(imageLocation, FileMode.Open, FileAccess.Read); 
    BinaryReader br = new BinaryReader(fs); 
    imageData = br.ReadBytes((int)imageFileLength); 
    return imageData; 
} 
+0

我沒有圖像的位置,我不保存它,我只是把選定的文件轉換成Buytes; –

0

如果我猜的話,該方法AddParams被一遍又一遍又一遍,其自稱就是爲什麼你收到StackOverFlowException: https://msdn.microsoft.com/en-us/library/system.stackoverflowexception(v=vs.110).aspx

+0

所以,我做了什麼代碼只能稱自己一次,我可以有多個Prams –

+0

我想你可能想看看這個:https://msdn.microsoft.com/en-us/library/system .data.sqlclient.sqlcommand.parameters(v = vs.110).aspx 我有一種感覺,你正在嘗試創建一個SqlCommand對象。您需要將對象添加到SqlCommand對象的屬性Parameters中 –

相關問題