文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > IT/计算机 > 计算机软件及应用 > 用二进制将图片存到数据库
侵权投诉

用二进制将图片存到数据库

protected void Button2_Click(object sender, EventArgs e)

{

//得到用户要上传的文件名

string strFilePathName = UP_FILE.PostedFile.FileName;

string strFileName = Path.GetFileName(strFilePathName);

int FileLength = UP_FILE.PostedFile.ContentLength;

if (FileLength <= 0)

return;

try

{//上传文件

Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组 Stream StreamObject = UP_FILE.PostedFile.InputStream; //建立数据流对像 //读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度

StreamObject.Read(FileByteArray, 0, FileLength);

//建立SQL Server链接

string strCon =

WebConfigurationManager.ConnectionStrings["SqlConnectionStringHT"].ToString(); SqlConnection Con = new SqlConnection(strCon);

// String SqlCmd = "INSERT INTO ImageStore (ImageData,

ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)";

String SqlCmd = "INSERT INTO ImageStore (ImageData) VALUES (@Image)"; SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);

CmdObj.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = FileByteArray;

CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = UP_FILE.PostedFile.ContentType; //记录文件类型

//把其它单表数据记录上传

CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;

//记录文件长度,读取时使用

CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = FileLength;

Con.Open();

第1页

你可能喜欢

热门文档

相关文档

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email