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

MYSQL
mysql 常用数据库语句 小练习
MYSQL ERROR 1045 (28000): Access denied for user (using password: YES)问题的解决
mysql 字符集的系统变量说明
MySQL 在触发器里中断记录的插入或更新?
将MySQL数据库移植为PostgreSQL
mysql 操作总结 INSERT和REPLACE
linux mysql忘记密码的多种解决或Access denied for user ''root''@''localhost''
运用mysqldump 工具时需要注意的问题
mysql 优化日记
MySQL 字符串函数大全
mysql 截取指定的两个字符串之间的内容
MySQL 备份还原数据库批处理
mysql 数据库中my.ini的优化 2G内存针对站多 抗压型的设置
Mysql 数字类型转换函数
mysql 动态生成测试数据
mysql 显示SQL语句执行时间的代码
mysql 设置查询缓存
MYSQL explain 执行计划
MySQL 有输入输出参数的存储过程实例
巧用mysql提示符prompt清晰管理数据库的方法

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 52 ::
收藏到网摘: 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;
  }