I have a PictureBox
and I want to save its picture in a SQL Server database into a VarBinary
column
我有一个PictureBox,我想将它在SQL Server数据库中的图片保存到VarBinary列中
2 个解决方案
#1
-2
I didnt exactly get what you meant but here is a piece of code you can use for inserting an image into database from picture box.
我并没有完全明白你的意思,但这里有一段代码可以用来从图片框中将图像插入数据库。
Image img = picturebox1.Image();
byte[] arr;
ImageConverter converter = new ImageConverter();
arr=(byte[])converter.ConvertTo(img, typeof(byte[]));
command.CommandText = "INSERT INTO ImagesTable (Image) VALUES('" + arr + "')";
command.CommandType = CommandType.Text;
command.ExecuteNonQuery();
#2
-1
whoops found it
哎呀找到了
this is the insert methodes
这是插入方法
public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
public static void InsertImageintoSqlDatabaseAsBinary(byte[] imgData)
{
using (SqlConnection sqlconnection = new SqlConnection(@"Data Source=.;Initial Catalog=VIP;Trusted_Connection=True;"))
{
sqlconnection.Open();
// Converts image file into byte[]
string insertXmlQuery = @"Insert Into ExpertsInfo (Image) Values(@Photo)";
// Insert Image Value into Sql Table by SqlParameter
SqlCommand insertCommand = new SqlCommand(insertXmlQuery, sqlconnection);
SqlParameter sqlParam = insertCommand.Parameters.AddWithValue("@Photo", imgData);
sqlParam.DbType = DbType.Binary;
insertCommand.ExecuteNonQuery();
}
}
and Use of it like below :
和使用如下:
private void button2_Click(object sender, EventArgs e)
{
Image img = picturebox.Image;
byte[] imgbyte= ImageToByte(img);
InsertImageintoSqlDatabaseAsBinary(imgbyte);
}
#1
-2
I didnt exactly get what you meant but here is a piece of code you can use for inserting an image into database from picture box.
我并没有完全明白你的意思,但这里有一段代码可以用来从图片框中将图像插入数据库。
Image img = picturebox1.Image();
byte[] arr;
ImageConverter converter = new ImageConverter();
arr=(byte[])converter.ConvertTo(img, typeof(byte[]));
command.CommandText = "INSERT INTO ImagesTable (Image) VALUES('" + arr + "')";
command.CommandType = CommandType.Text;
command.ExecuteNonQuery();
#2
-1
whoops found it
哎呀找到了
this is the insert methodes
这是插入方法
public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
public static void InsertImageintoSqlDatabaseAsBinary(byte[] imgData)
{
using (SqlConnection sqlconnection = new SqlConnection(@"Data Source=.;Initial Catalog=VIP;Trusted_Connection=True;"))
{
sqlconnection.Open();
// Converts image file into byte[]
string insertXmlQuery = @"Insert Into ExpertsInfo (Image) Values(@Photo)";
// Insert Image Value into Sql Table by SqlParameter
SqlCommand insertCommand = new SqlCommand(insertXmlQuery, sqlconnection);
SqlParameter sqlParam = insertCommand.Parameters.AddWithValue("@Photo", imgData);
sqlParam.DbType = DbType.Binary;
insertCommand.ExecuteNonQuery();
}
}
and Use of it like below :
和使用如下:
private void button2_Click(object sender, EventArgs e)
{
Image img = picturebox.Image;
byte[] imgbyte= ImageToByte(img);
InsertImageintoSqlDatabaseAsBinary(imgbyte);
}