您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2025/12/29 18:01:42发布14次查看
图片通过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;
}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product