C#中从数据库下载文件
发布网友
发布时间:2022-05-07 03:44
我来回答
共2个回答
热心网友
时间:2023-06-29 12:21
首先要把数据库里面对应的那个字段读出来 然后进行处理
下面的例子是我从数据库读取图片的
OracleConnection conn = new OracleConnection(connstring);
conn.Open();
label1.Text = "开始时间:" + DateTime.Now;
string begin = "数据库连接正常,开始时间" + DateTime.Now;
SetInfo("", begin);
string SQL = "select ID from bs_buylicense_attach t where t.file_size>204800 order by t.file_size asc"; //rownum<100 and
string ConnString = SQL;
OracleDataAdapter sda = new OracleDataAdapter(ConnString, conn);
DataTable table = new DataTable();
sda.Fill(table);
List<object> lst = ModelConvertHelper.DtCovertIList(table);
foreach (object o in lst)
{
string path = "C:\\1.png"; ;
string lst_ID = o.ToString(); //查询返回来对应的数据的ID
string filter = "select * from bs_buylicense_attach where ID='" + lst_ID + "'";
OracleCommand cmd = new OracleCommand(filter, conn);
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
if (reader["DATA"] != DBNull.Value)
{
string ID = "数据ID对应值ID " + reader["ID"].ToString();
string length = "原文件大小" + reader["FILE_SIZE"].ToString();
SetInfo(ID, length);
System.IO.MemoryStream stream = new System.IO.MemoryStream((byte[])reader["DATA"]);//把照片读到MemoryStream里
Image ImageBlob = Image.FromStream(stream, true); //从数据库中读到的图片
int height = ImageBlob.Height; //原来图片的高度
int width = ImageBlob.Width;//原来图片的宽度
//开始进行图片的压缩
GetPicThumbnail1(ImageBlob, path, height, width, 60);
Image SaveImage = im; //得到返回来压缩之后的图片
byte[] buffer = imageToByteArray(SaveImage);
string UpdateSql = "update bs_buylicense_attach set DATA =:image where ID='" + lst_ID + "'";
OracleCommand cd = new OracleCommand();
cd = new OracleCommand(UpdateSql, conn);
cd.CommandText = UpdateSql;
cd.Parameters.Add("image", System.Data.OracleClient.OracleType.Blob, buffer.Length).Value = buffer;
cd.ExecuteNonQuery();
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
string YAsuo = Convert.ToString(file.Length);
SetInfo("压缩为:", YAsuo);
stream.Close();
stream.Dispose();
file.Dispose();
file.Close();
File.Delete(@path);
}
}
}
conn.Close();
string end = "所有数据执行完毕关闭数据库链接!" + DateTime.Now;
SetInfo("", end);
SaveLog();
label2.Text = "结束时间:" + DateTime.Now;
}
热心网友
时间:2023-06-29 12:21
数据库文件是保存为二进制格式的,从数据库中查询得到数据后,写成对应格式的文件就可以了