需求:旧系统数据库 图片存的是进制。数据有13w条(历史原因)。需要把图片进制转换对应的图片保存到本地 新系统保存图片地址。。现在写一个小程序需要转换旧系统图片数据 winfrom while (_isWhile) { sql= txtSql.Text.Replace(row, _nextRow).Replace(pagesize, txtRow.Text); Logging.Instance.WriteEnqueue(null, sql); var dt = GetImgData(sql); if (dt == null) { //没有数据自动终止循环 _isWhile = false; lblMsg.Text = "执行完成"; Logging.Instance.WriteEnqueue(null, lblMsg.Text); return ; } for (int i = 0; i < dt.Rows.Count; i++) { 目前是分批读取数据 。但是这里 Bitmap 这块这样写我知道也有问题 不停的创建对象。 内存溢出。如何解决呢?
_indexSuccess = 0; BtnImg.Enabled = false; if (BtnImgVerification()) return; Logging.Instance.CreateFile(txtPath.Text); lblMsg.Text = "程序执行中......."; Task.Factory.StartNew(() => { ExceptionsHelp.Instance.ExecuteVoidFunc(() => { var dr = DbHelperSql.ExecuteReader(txtSql.Text); var ms = new MemoryStream(); while (dr.Read()) { var imgName = dr[txtFileName.Text]; var stream = dr[txtFileStream.Text]; if (string.IsNullOrEmpty(stream.ToString())) { Logging.Instance.WriteEnqueue(null, imgName + "对应列数据为空"); continue; } byte[] imagebytes = (byte[])stream; var filename = txtPath.Text + "\\" + imgName + ".jpg"; ms.Write(imagebytes, 0, imagebytes.Length); using (FileStream fsWrite = new FileStream(filename, FileMode.Append)) { fsWrite.Write(imagebytes, 0, imagebytes.Length); }; ms.Position = 0; _indexSuccess++; } ms.Dispose(); ms.Close(); dr.Close(); GC.Collect(); Logging.Instance.WriteEnqueue(null, _indexSuccess.ToString()); MessageBox.Show(_indexSuccess.ToString()); }, "BtnImg_Click", () => { Method(BtnImg, x => x.Enabled = true); }); });
|
|