asp.net 将图片上传到mysql数据库的方法
时间:2020-10-14 10:04:57|栏目: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;
}
复制代码 代码如下:
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;
}
栏 目:Mysql
下一篇:解析Mysql备份与恢复简单总结与tee命令的使用介绍
本文标题:asp.net 将图片上传到mysql数据库的方法
本文地址:http://www.codeinn.net/misctech/11128.html