发生“参数无效”错误是因为您从数据库中取回的字节数组不代表有效的图像二进制数据。
发生这种情况的原因有多种。首先,查看数据库中的数据——它可能没有存储您期望的二进制数据。
与此同时,我快速拼凑了一个小型控制台应用程序,可以满足您的需求:
class LoadImageFromDbSpike
{
private const string ConnectionString = @"Data Source=[SERVERNAME];Initial Catalog=[DBNAME];Trusted_Connection=true;Connect Timeout=180;";
private const string ImageOnDiskPath = @"c:\test.png";
private const string OutputPath = @"c:\output.png";
static void Main(string[] args)
{
var imageData = File.ReadAllBytes(ImageOnDiskPath);
var imageId = StoreImageData(imageData);
var imageDataFromDb = LoadImageData(imageId);
File.WriteAllBytes(OutputPath, imageDataFromDb);
}
private static int StoreImageData(IEnumerable<byte> imageData)
{
const string insertCommand = "INSERT INTO ImageSpike (Image) " +
"VALUES (@ImageData); " +
"SELECT SCOPE_IDENTITY();";
using (var con = new SqlConnection(ConnectionString))
using (var cmd = new SqlCommand(insertCommand, con))
{
cmd.Parameters.AddWithValue("@ImageData", imageData);
cmd.Connection.Open();
return (int) (decimal) cmd.ExecuteScalar();
}
}
private static byte[] LoadImageData(int id)
{
const string loadImageCommand = "SELECT TOP 1 Image FROM ImageSpike " +
"WHERE Id = @Id; ";
using (var con = new SqlConnection(ConnectionString))
using (var cmd = new SqlCommand(loadImageCommand, con))
{
cmd.Parameters.AddWithValue("@Id", id);
cmd.Connection.Open();
var result = cmd.ExecuteScalar();
return (byte[]) result;
}
}
}