怎样把用FileUpload控件上传的图片保存到数据库里?各位大虾帮帮小弟

cl981752069 2009-07-29 02:27:48
我用了一个FileUpload控件上传图片,和有一个button 按钮,想实现当点击button时 把图片保存到数据库里
最好有代码
...全文
146 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cl981752069 2009-07-29
  • 打赏
  • 举报
回复
先谢了 我试试
lihan6415151528 2009-07-29
  • 打赏
  • 举报
回复
就是保存图片路径就行了 文件你可以上传到服务器
cpp2017 2009-07-29
  • 打赏
  • 举报
回复
protected   void   Button1_Click(   object   sender,   EventArgs   e   ) 
{
System.IO.Stream fileDataStream = FileUpload1.PostedFile.InputStream;

if (fileDataStream.Length < 1)
{
Msg.Text = "请选择文件。 ";
return;
}

//得到文件大小
int fileLength = FileUpload1.PostedFile.ContentLength;

//创建数组
byte[] fileData = new byte[fileLength];
//把文件流填充到数组
fileDataStream.Read(fileData, 0, fileLength);
//得到文件类型
string fileType = FileUpload1.PostedFile.ContentType;

//构建数据库连接,SQL语句,创建参数
string strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Server.MapPath( "Image2Access.mdb ");
OleDbConnection myConnection = new OleDbConnection(strCnn);
OleDbCommand command = new OleDbCommand( "INSERT INTO Person (PersonName,PersonEmail,PersonSex,PersonImageType,PersonImage) " +
"VALUES (@PersonName,@PersonEmail,@PersonSex,@PersonImageType,@PersonImage) ", myConnection);

command.Parameters.AddWithValue( "@PersonName ",TextBox1.Text);
command.Parameters.AddWithValue( "@PersonEmail ", "mengxianhui@dotnet.aspx.cc ");
command.Parameters.AddWithValue( "@paramPersonSex ", "男 ");
command.Parameters.AddWithValue( "@PersonImageType ", fileType);
command.Parameters.AddWithValue( "@PersonImage ", fileData);


//打开连接,执行查询
myConnection.Open();
command.ExecuteNonQuery();
myConnection.Close();

28,409

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧