您的位置:首页 > 编程语言 > ASP

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());

}

}

有错误大家指正..
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: