本文介绍了如何使用 C# 将 PictureBox 插入到 Sql Server 数据库 Varbinary(MAX)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表的字段为 Varbinary(MAX) 数据类型,我想将 PictureBox 中的图像文件插入此列.那么如何在 Sql Server 中将图像转换并插入到 Varbinary 中.谢谢你的帮助.
I have a table that have a field as Varbinary(MAX) datatype and I want to insert image file from PictureBox into this column. So how can I convert and insert image to Varbinary in Sql Server. Thank for help me.
推荐答案
你真的应该展示一些代码,你尝试了什么......
You should really show some code, what have you tried...
这只是一个猜测,但它应该给你一个如何将图片插入数据库的线索:
This is only a guess, but it should give you a clue how to insert picture into database:
//byte array that will hold image data
byte[] imageData = null;
using (var ms = new MemoryStream())
{
//here is image property of your pictureBox control saved into memory stream
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
imageData = ms.ToArray();
}
//make sql connection
SqlConnection conn = new SqlConnection("your connection string goes here");
// command with parameter
SqlCommand cmd = new SqlCommand("insert into TableWithImages (imageData) values (@imageData);", conn);
//define param and pass byte array as value
cmd.Parameters.Add("@imageData", SqlDbType.VarBinary).Value = imageData;
//do insert
cmd.ExecuteNonQuery();
这篇关于如何使用 C# 将 PictureBox 插入到 Sql Server 数据库 Varbinary(MAX)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!