ASP.Net 照片数据(二进制数据)读取和写入简单举例
来源:互联网 发布:ubuntu xrdp无法连接 编辑:程序博客网 时间:2024/05/08 11:09
//保存照片数据
public int SavePhoto(string photeFile)
{
//读取文件流
FileStream fs = new FileStream(photeFile,FileMode.Open,FileAccess.Read);
BinaryReader reader =new BinaryReader(fs);
byte[] photo = reader.ReadBytes((int)fs.Length);
reader.Close();
fs.Close();
string sql = "update student set zhaopian=@zhaopian where xuehao='001'";
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection();
conn.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";
SqlCommand command = new SqlCommand(sql, conn);
SqlParameter parm = new SqlParameter("@zhaopian", photo);
command.Parameters.Add(parm);
try
{
conn.Open();
return command.ExecuteNonQuery();
}
catch (SqlException ex)
{
return -1;
}
finally
{
conn.Close();
}
}
//读取二进制数据 重新保存为新文件,新文件和旧文件大小相同
public void ReadPhoto(string photeFile)
{
string sql = "select zhaopian from student where xuehao='001'";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";
SqlCommand command = new SqlCommand(sql,conn);
try
{
conn.Open();
SqlDataReader reader = command.ExecuteReader();
while(reader.Read())
{
byte[] photo = reader[0] as byte[];
FileStream fs = new FileStream(photeFile,FileMode.CreateNew);
fs.Write(photo,0,photo.Length);
fs.Close();
}
reader.Close();
}
catch (SqlException ex)
{
}
finally
{
conn.Close();
}
}
- ASP.Net 照片数据(二进制数据)读取和写入简单举例
- asp.net 读取读取sql数据并写入txt文档
- Oracle中如何读取和写入二进制数据
- IDL/C 二进制数据的读取和写入
- asp.net中XML文档的写入和读取,以及转化数据集相关实例
- Java 读取 C++写入的二进制数据
- 在 ASP 中读取和显示二进制数据
- .net(NPOI)实现Excel数据的写入和读取
- json读取和写入数据
- R数据读取和写入
- asp.net 二进制数据写入及读出以 图片方式展现
- 注册表写入二进制数据
- 15.3 写入二进制数据
- Postgresql快速写入\/读取大量数据(.net)
- Postgresql快速写入/读取大量数据(.net)
- .Net读取Excle数据写入到数据库
- 写入和读取图片(c# asp。net sqlserver)
- 写入和读取图片(c# asp.net sqlserver)
- arm中断分析
- RadioGroup & RadioButton OnCheckedChangeListener
- 建立ssh-key
- 图像文件格式解析(2)
- winpcap学习(五)
- ASP.Net 照片数据(二进制数据)读取和写入简单举例
- NYOJ864 统计
- linux 常用查看设备命令
- 图片格式详解(1)
- VS2010中GetMenu()和GetSubMenu(0)为NULL引发异常的解决方法
- linux常用命令汇总(一)
- 读小未博客2dToolkit实现屏幕照片移动有感
- 我所认识的约翰纳什
- linux常用命令汇总(二)