asp.net 上传图片到数据库(不是文件夹)
2012-01-24 11:40
387 查看
protected void Button1_Click(object sender, EventArgs e)
{
try
{
String imgPath = FileUpload1.PostedFile.FileName;
String imgName = imgPath.Substring(imgPath.LastIndexOf("\\")+1);
String imgExtend = imgPath.Substring(imgPath.LastIndexOf(".")+1);
int FileLen = this.FileUpload1.PostedFile.ContentLength;
Byte[] FileData=new Byte[FileLen];
HttpPostedFile hp = FileUpload1.PostedFile;
Stream sr = hp.InputStream;
sr.Read(FileData,0,FileLen);
SqlConnection con = new SqlConnection("server=(local);user id=zheng;pwd=zheng;database=aa");
con.Open();
SqlCommand com = new SqlCommand("insert into Files(FileData)values(@imgdata)",con);
com.Parameters.Add("@imgdata",SqlDbType.Image);
com.Parameters["@imgdata"].Value = FileData;
com.ExecuteNonQuery();
}
catch(Exception error)
{
Response.Write("上传失败"+error.ToString());
}
}
有错误大家指正..
{
try
{
String imgPath = FileUpload1.PostedFile.FileName;
String imgName = imgPath.Substring(imgPath.LastIndexOf("\\")+1);
String imgExtend = imgPath.Substring(imgPath.LastIndexOf(".")+1);
int FileLen = this.FileUpload1.PostedFile.ContentLength;
Byte[] FileData=new Byte[FileLen];
HttpPostedFile hp = FileUpload1.PostedFile;
Stream sr = hp.InputStream;
sr.Read(FileData,0,FileLen);
SqlConnection con = new SqlConnection("server=(local);user id=zheng;pwd=zheng;database=aa");
con.Open();
SqlCommand com = new SqlCommand("insert into Files(FileData)values(@imgdata)",con);
com.Parameters.Add("@imgdata",SqlDbType.Image);
com.Parameters["@imgdata"].Value = FileData;
com.ExecuteNonQuery();
}
catch(Exception error)
{
Response.Write("上传失败"+error.ToString());
}
}
有错误大家指正..
相关文章推荐
- asp.net图片上传 ----上传至数据库---上传至项目文件夹
- 上传图片到数据库--ASP.NET中常用代码之三 (转载)
- asp.net下CKFinder IE9以下浏览器中上传图片文件时提示“无效文件名或文件夹名称”的解决方法
- asp.net 批量上传图片保存到数据库
- ASP.NET C#(4.0) + SQL2008 R2 多图片上传并生成缩略图一并保存至数据库
- asp.net上传图片保存到数据库的代码
- 用asp.net实现将上传的图片变小存入数据库!(暑假里就开始想做的,很兴奋了)
- ASP.NET 上传图片至数据库并读取图片显示
- ASP.NET上传图片到数据库
- asp.net上传图片保存到数据库的代码
- ASP.net后台程序中的图片上传到数据库中的路径问题
- ASP.NET中常用功能代码总结——上传图片到数据库
- asp.net图片上传并存储到数据库中
- asp.net将上传图片打上防伪图片水印并写入数据库
- asp.net对数据库中图片的上传与读取
- ASP.NET下上传图片到数据库,并且读出图片的代码(详细版)
- ASP.NET中常用功能代码总结(3)——上传图片到数据库
- ASP.NET中常用功能代码总结(上传图片到数据库,从数据库中读取图片)
- 实现ASP.NET数据库图片上传与读取的功能