如何将图像插入数据库中的表 [英] How to insert an image to a table in a database
本文介绍了如何将图像插入数据库中的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将图片框中的图像插入数据库的表中?
How can a image in a picture box be insert into a table in the database?
private void button3_Click(object sender, EventArgs e) //Add button
{
if (textBox1.Text == "" ||textBox2.Text==""|| pictureBox1.Container == null || pictureBox2.Container == null)
MessageBox.Show("Please make sure to enter the name and pictures!!");
else//add pictures to table
{
try
{
string dbconnstr = @"C:\Users\compaq\Desktop\new\n1\ClassLibrary1\Database1.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(dbconnstr);
conn.Open();
string qry = "insert into pic values (pictureBox1.Image)";
//Initialize SqlCommand object for insert.
SqlCommand sqlcom = new SqlCommand(qry,conn);
sqlcom.BeginExecuteNonQuery();
conn.Close();
conn.Dispose();
}
catch (Exception ee)
{string error = ee.Message;}
}
此代码不会在表中插入图片,即使没有错误也是如此.
请帮忙.
This code doesn''t insert pictures to table, even there are no errors to be found.
Please help.
推荐答案
尝试一下
C#从数据库保存和加载图像 [
Try this
C# Save and Load Image from Database[^]
尝试此 ^ ]和 C#从以下位置保存和加载图像数据库 [ ^ ]
CP上有很多文章可以为您提供帮助.尝试搜索:-)
Try this Load/Unload images into/from DB table[^] and C# Save and Load Image from Database[^]
There are many articles here at CP that can help you. Try search :-)
不.不会的为什么?因为"picturebox1.Image"是一个字符串.不是图像.
如果您在数据库中配置了该字段,请尝试:
Nope. It won''t. Why? because "picturebox1.Image" is a string. Not an image.
If you have the field configured in your database, then try:
SqlConnection conn = new SqlConnection(dbconnstr);
conn.Open();
string qry = "INSERT INTO myTable (myImageField) VALUES (@PD)";
//Initialize SqlCommand object for insert.
SqlCommand sqlcom = new SqlCommand(qry,conn);
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
sqlcom.Parameters.AddWithValue("@PD", ms.ToArray());
sqlcom.ExecuteNonQuery();
这篇关于如何将图像插入数据库中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文