首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET Framework >

文件保存到数据库 内存泄露 求教解决思路

2012-04-02 
文件保存到数据库 内存泄露 求教没上传一个文件总会有约100M内存没有被释放掉 求原因//上传private void b

文件保存到数据库 内存泄露 求教
没上传一个文件总会有约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);


  saveDialog.FileName = strFileName;
  saveDialog.Filter = "*." + sdr.GetString(1) +"|*.*"; //"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
  if (saveDialog.ShowDialog() == DialogResult.OK)
  {
  strFileName = saveDialog.FileName;
  WriteFile(bytes, strFileName);
  }
  }
  }
  }
  }
//=====================数据库操作类=============================
public class DB
  {
  private string _ConStr = string.Empty;
  private SqlConnection _Conn;
  public DB()
  {
  string _server = ConfigurationManager.AppSettings["Server"];
  string _uid = ConfigurationManager.AppSettings["User"];
  string _pwd = ConfigurationManager.AppSettings["Password"];
  string _database = ConfigurationManager.AppSettings["Database"];
  _ConStr = "server=" + _server + ";uid=" + _uid + ";pwd=" + _pwd + ";database=" + _database;
  GetConn();
  }

  /// <summary>
  /// 实例化数据库连接
  /// </summary>
  public void GetConn()
  {
  _Conn = new SqlConnection(_ConStr);
   
  }
  /// <summary>
  /// 对数据库查询 
  /// </summary>
  /// <param name="Sql"> SQL查询语句 string类型</param>
  /// <returns>返回一张表 DataTable</returns>
  public DataTable GetTable(string Sql)
  {
  _Conn.Open();
  SqlDataAdapter DA = new SqlDataAdapter(Sql, _Conn);
  DataSet DS = new DataSet();
  DA.Fill(DS);
  _Conn.Close();
  DataTable DTable = DS.Tables[0];
  return DTable;
  }

  public SqlDataReader GetReader(string Sql)
  {
  try
  {
  _Conn.Open();
  SqlCommand Cmd = new SqlCommand(Sql, _Conn);
  SqlDataReader Reader = Cmd.ExecuteReader();
  return Reader;
  }
  finally
  {
  //_Conn.Close();
  }
  }

  /// <summary>
  /// 用现有的数据库连接执行一个sql命令(不返回数据集)
  /// </summary>
  /// <param name="conn">一个现有的数据库连接</param>
  /// <param name="commandType">命令类型(存储过程, 文本, 等等)</param>
  /// <param name="commandText">存储过程名称或者sql命令语句</param>
  /// <param name="commandParameters">执行命令所用参数的集合</param>
  /// <returns>执行命令所影响的行数</returns>
  public int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
  {
  using (SqlCommand cmd = new SqlCommand())
  {
  PrepareCommand(cmd, _Conn, null, cmdType, cmdText, commandParameters);
  int val = cmd.ExecuteNonQuery();
  cmd.Parameters.Clear();
  _Conn.Close();
  _Conn.Dispose();


  return val;
  }
  }

  /// <summary>
  /// 准备执行一个命令
  /// </summary>
  /// <param name="cmd">sql命令</param>
  /// <param name="conn">Sql连接</param>
  /// <param name="trans">Sql事务</param>
  /// <param name="cmdType">命令类型例如 存储过程或者文本</param>
  /// <param name="cmdText">命令文本,例如:Select * from Products</param>
  /// <param name="cmdParms">执行命令的参数</param>
  private void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
  {

  if (conn.State != ConnectionState.Open)
  conn.Open();

  cmd.Connection = conn;
  cmd.CommandText = cmdText;

  if (trans != null)
  cmd.Transaction = trans;

  cmd.CommandType = cmdType;

  if (cmdParms != null)
  {
  foreach (SqlParameter parm in cmdParms)
  cmd.Parameters.Add(parm);
  }
  }
  }

[解决办法]
txbFileFullPath.Text是客户端路径,你这样写完全错误的。、保存数据库的方法

http://dotnet.aspx.cc/article/17612afb-3fc0-4fb9-bfa9-00cba28336e9/read.aspx
[解决办法]
是否关闭conn.close()???你只是自动关闭了fs,,

热点排行