日期:2014-05-20  浏览次数:20932 次

文件保存到数据库 内存泄露 求教
没上传一个文件总会有约100M内存没有被释放掉 求原因  
  //上传
  private void btnSubmit_Click(object sender, EventArgs e)
  {
  //txbFileFullPath.Text 文件全路径
  using (FileStream fs = new FileStream(txbFileFullPath.Text, FileMode.Open))
  {
  byte[] bytes;
  try
  {
  bytes = new byte[fs.Length];
  fs.Read(bytes, 0, bytes.Length);
  }
  catch (Exception ex)
  {
  MessageBox.Show("错误信息:" + ex.Message, "文件读取出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
  return;
  }
  string strSQL = "Insert into FileData ( [FileName], Filter, FileBytes, UpLoadTime ) values ( @FileName, @Filter, @FileBytes, @UpLoadTime);";
  SqlParameter[] cmdParms = new SqlParameter[]
  {
  new SqlParameter("@FileName",SqlDbType.VarChar),
  new SqlParameter("@Filter", SqlDbType.VarChar),
  new SqlParameter("@FileBytes", SqlDbType.VarBinary),
  new SqlParameter("@UpLoadTime",SqlDbType.DateTime)
  };
  cmdParms[0].Value = txbFileName.Text;
  cmdParms[1].Value = lbFileType.Text;
  cmdParms[2].Value = bytes;
  cmdParms[3].Value = DateTime.Now;
  DB db = new DB();
  db.ExecuteNonQuery( CommandType.Text, strSQL, cmdParms);
  fs.Dispose();
  btnSubmit.Enabled = false;
  }
  }

  /// <summary>
  /// 生成文件
  /// </summary>
  /// <param name="pReadByte">二进制流</param>
  /// <param name="fileName">文件全路径</param>
  private void WriteFile(byte[] pReadByte, string fileName)
  {
  FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate);
  try
  {
  fs.Write(pReadByte, 0, pReadByte.Length);

  }
  catch (Exception ex)
  {
  MessageBox.Show("错误信息:" + ex.Message, "文件生成出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
  return;
  }
  finally
  {
  if (fs != null)
  fs.Close();
  }

  }

  //下载
  private void btnDownLoad_Click(object sender, EventArgs e)
  {

  string strFileName = string.Empty;

  string sql = " Select [FileName], Filter, FileBytes from FileData where ID = " + txbFileID.Text.Trim();

  DB db = new DB();
  //数据表
  using (SqlDataReader sdr = db.GetReader(sql))
  {

  while (sdr.Read())
  {
  strFileName = sdr.GetString(0);
  byte[] bytes = (byte[])sdr[2];
  //保存窗体
  using (SaveFileDialog saveDialog = new SaveFileDialog())
  {
  saveDialog.CheckPathExists = true;
  saveDialog.DefaultExt = sdr.GetString(1);