Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将SQL varbinary转换为图像和接收参数无效_C# - Fatal编程技术网

C# 将SQL varbinary转换为图像和接收参数无效

C# 将SQL varbinary转换为图像和接收参数无效,c#,C#,我试图从SQL中提取数据,并将其显示在windows窗体上的picturebox中 守则: // Use the customer number to obtain image from the DBFile table Query.CommandText = "SELECT * FROM DBFile " + "WHERE " + "FileName = @FileName"; // Fill in the parameters Query.Parameters.AddWit

我试图从SQL中提取数据,并将其显示在windows窗体上的picturebox中

守则:

// Use the customer number to obtain image from the DBFile table
Query.CommandText = "SELECT * FROM DBFile " +
    "WHERE " +
    "FileName = @FileName";

// Fill in the parameters
Query.Parameters.AddWithValue("@FileName", customerNumber + ".jpg");

// Build the DBFile data table
da = new SqlDataAdapter(Query);
cb = new SqlCommandBuilder(da);
da.Fill(DBFile);

// Dispose of SQL data
da.Dispose();
cb.Dispose();
Query.Dispose();

try
{
    byte[] bImage = new byte[0];
    bImage = (byte[])DBFile.Rows[0]["Data"];
    MemoryStream ms = new MemoryStream(bImage);
    pbLicense.Image = Image.FromStream(ms); // Error is thrown here
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

我的问题是,这不仅是获取所需数据的正确方法,而且是将其转换为所需结果的正确方法吗?如果是,我缺少什么?。我看过很多教程和指南,但在我尝试的方面似乎都很相似。

抛出了什么错误?为什么字节数组的大小为0?参数无效。字节数组填充在下面的行中--我也使用了这个数字,但没有用。您的代码还有其他问题:1)不要使用
ex.Message
-它会遗漏重要信息。2) 您的
SqlDataAdapter
MemoryStream
,甚至更多,应该在
中使用
块,以便在发生异常时仍能处理它们。您是如何将图像字节放入数据库的?如果你保存了错误的东西,那么你可能加载了错误的东西。数据库已经用已经存在的数据创建了——我正在尝试从中提取和转换数据。关于数据库,更具体地说,表和匹配列的唯一细节是数据类型:varbinary(max)。