1. 将winform压缩包转换为二进制数厅岁据,使用流或文件流将文件读伏伏基取到内存中,然后通过System.Convert.ToBase64String()将其转换为二进制字符串;
2. 将二进制字符串存入数据库中,可以使用SqlCommand对象,将二进制字符串插入到指定的数据库表中;
3. 从数据库中读取winform压缩包,使用SqlDataReader对象,读取指定的表中的二进制字符串,然后使用System.Convert.FromBase64String()将其转换为流或文缺谨件流;
4. 将流或文件流写入到本地文件中,即可获取winform压缩包。
首先..定义一个函数..将图片转化为二进制码//定义将图岁族衡片转化为长二进制代码的函数getphoto()
public Byte[] getphoto(string photopath)
{
string str = photopath
FileStream file = new FileStream(str, FileMode.Open, FileAccess.Read)
Byte[] bytBLOBData = new Byte[file.Length]
file.Read(bytBLOBData, 0, bytBLOBData.Length)
file.Close()
return bytBLOBData
}//这是定义函数..
然后..就是将转换成二进制码的图片插入数据库乎做中..下面是简单的也是重要的sql语句..
if (this.pictureBox1.Image != null)
{
sql1 = sql1 + ",Photo"穗并
sql2 = sql2 + ",bytBLOBData"
Byte[] bytBLOBData = getphoto(openFileDialog1.FileName)
cmd.Parameters.Add(new OleDbParameter("jpeg", OleDbType.Binary, bytBLOBData.Length, ParameterDirection.Input, true, 0, 0, null, DataRowVersion.Default, bytBLOBData))
}
接下来..是读取...
string sql = "select photo from studentinfo where studentid = " + this.Tag.ToString()
OleDbCommand cmd = new OleDbCommand(sql, connection1)
if (Convert.DBNull != cmd.ExecuteScalar())
pictureBox1.Image = Image.FromStream(new MemoryStream((Byte[])cmd.ExecuteScalar()))//读取长二进制为图片..
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)