当前位置: 首页 > 图文教程 > 数据库 > MYSQL > asp.net 将图片上传到mysql数据库的方法

MYSQL
Access数据库的存储上限
一步一步教你网站同步镜像(转载)
MYSQL大数据导入
在同一台机器上运行多个 MySQL 服务
Mysql服务器的启动与停止(一)
Mysql服务器的启动与停止(二)
MySQL新手入门指南--快速参考
从MySQL得到最大的优化性能
数据库的用户帐号管理基础知识
如何恢复MYSQL的ROOT口令
MySQL优化全攻略-相关数据库命令
MySQL 常用命令
MySQL 管理
MySQL SQL 语法参考
SQL 优化
MySQL 索引分析和优化
Advanced SQL Injection with MySQL
ADODB 入门
MySQL 数据库函数库
使用distinct在mysql中查询多条不重复记录值的解决办法

MYSQL 中的 asp.net 将图片上传到mysql数据库的方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 61 ::
收藏到网摘: n/a

图片通过asp.net上传到mysql数据库的方法 这是页面上的按钮单击事件
复制代码 代码如下:

  protected void Button1_Click(object sender, EventArgs e)
  {
  string tid = Utils.getRandom(32);
  Stream mystream = this.FileUpload1.PostedFile.InputStream;
  int length = this.FileUpload1.PostedFile.ContentLength;
  byte[] pic = new byte[length];
  mystream.Read(pic, 0, length);
  bool flg = insert(tid, pic);
  }

  这是执行插入的方法
复制代码 代码如下:
 
 public bool insert(string tid,byte[] pic)
  {
  DBConn db = new DBConn();
  StringBuilder sql = new StringBuilder();
  sql.Append("insert into teacher(TID,TPHOTO,TDELETE) values (?tid,?pic,?flg)");
  int flg = 0;
  try
  {
  myConnection = db.getConnection();
  MySqlCommand myCommand = new MySqlCommand(sql.ToString(), myConnection);
  myCommand.Parameters.Add(new MySqlParameter("?tid", MySqlDbType.String, 32));
  myCommand.Parameters["?tid"].Value = tid;
  myCommand.Parameters.Add(new MySqlParameter("?pic", MySqlDbType.Blob));
  myCommand.Parameters["?pic"].Value = pic;
  myCommand.Parameters.Add(new MySqlParameter("?flg", MySqlDbType.Int16));
  myCommand.Parameters["?flg"].Value = 0;
  myConnection.Open();
  flg = myCommand.ExecuteNonQuery();
  }
  catch (Exception ex)
  {
  return false;
  }
  finally
  {
  if (myConnection != null)
  {
  myConnection.Close();
  }
  }
  if (flg > 0)
  {
  return true;
  }
  return false;
  }