我有一个用 WPF 3.5 编写的应用程序,它在某些时候将数据保存在 SQL Server 中包含图像中,这是保存数据的代码的一部分(注意 this.pictureImage 是一个 WPF Image 控件):-
I have an application written in WPF 3.5 which at some point it saves the data in including an image in SQL Server, this is part of the code in saving the data (note this.pictureImage is a WPF Image control):-
using (SqlCommand command = myConnection.CreateCommand())
{
String sqlInsertCommand = "INSERT INTO Info_Id (idNumber, FirstName, Nationality, Image) VALUES (@idNumber, @firstName, @nationality, @image)";
command.CommandText = sqlInsertCommand;
command.CommandType = System.Data.CommandType.Text;
command.Parameters.AddWithValue("@idNumber", this.cardIdTextBlock.Text);
command.Parameters.AddWithValue("@firstName", this.fullNameTextBlock.Text);
command.Parameters.AddWithValue("@nationality", this.nationaltyTextBlock.Text);
command.Parameters.AddWithValue("@image", this.pictureImage);
command.ExecuteNonQuery();
}
运行此程序并单击保存到数据库按钮后,出现以下错误.
After I run this and click on the saving to DB button I got the following error.
不存在从对象类型 System.Windows.Controls.Image 到已知托管提供程序的映射
No mapping exists from object type System.Windows.Controls.Image to a known managed provider
在 SQL Server 数据库中,我有一行 (Picture (Image, null)).
In the SQL Server database I have a row with (Picture (Image, null)).
请多多指教.谢谢.
您无法在数据库中保存 Image 控件.相反,您应该通过适当的位图编码器在 Image 控件的 Source 属性中对图像进行编码,并将编码的缓冲区存储为字节数组.
You can't save an Image control in your database. Instead you should encode the image in the Image control's Source property by an appropriate bitmap encoder and store the encoded buffer as byte array.
byte[] buffer;
var bitmap = pictureImage.Source as BitmapSource;
var encoder = new PngBitmapEncoder(); // or one of the other encoders
encoder.Frames.Add(BitmapFrame.Create(bitmap));
using (var stream = new MemoryStream())
{
encoder.Save(stream);
buffer = stream.ToArray();
}
// now store buffer in your DB
这篇关于将图像从 WPF 控件保存到 SQL Server DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
我可以在不编写 SQL 查询的情况下找出数据库列表Can I figure out a list of databases and the space used by SQL Server instances without writing SQL queries?(我可以在不编写 SQL 查询的情况下
如何创建对 SQL Server 实例的登录?How to create a login to a SQL Server instance?(如何创建对 SQL Server 实例的登录?)
如何通过注册表搜索知道SQL Server的版本和版本How to know the version and edition of SQL Server through registry search(如何通过注册表搜索知道SQL Server的版本和版本)
为什么会出现“数据类型转换错误"?使用 ExWhy do I get a quot;data type conversion errorquot; with ExecuteNonQuery()?(为什么会出现“数据类型转换错误?使用 ExecuteNonQuery()?)
如何将 DataGridView 中的图像显示到 PictureBox?How to show an image from a DataGridView to a PictureBox?(如何将 DataGridView 中的图像显示到 PictureBox?)
WinForms 应用程序设计——将文档从 SQL Server 移动WinForms application design - moving documents from SQL Server to file storage(WinForms 应用程序设计——将文档从 SQL Server 移动到文件存