2017-09-26 109 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中 –

相关问题