怎么将pictureBox中的图片写入数据库呢?

来源:百度知道 编辑:UC知道 时间:2024/05/29 23:51:48
不知道怎么转二进制流哇。。
SQL

用blob,什么数据库呢,sql还是oracle?

FileStream fs = File.OpenRead(pictureBox1.ImageLocation);

byte[] content = new byte[fs.Length];
fs.Read(content, 0, content.Length);
fs.Close();
SqlConnection conn = new SqlConnection(你的连接字符串);
conn.Open();
string sql = 你的Insert语句,这个要你自己写了,我不知道表结构

SqlCommand comm = new SqlCommand(sql, conn);
comm.Parameters.Add("@你的图片字段", SqlDbType.Image).Value = content;
if (comm.ExecuteNonQuery() == 1)
{
MessageBox.Show("保存成功");
}
conn.Close();

这就可以了^_^

你可以把图片放在一个文件夹中,在数据库中只存路径,等用时再取出来.直接放进数据库,操作可能会麻烦一点.