博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
看不懂 ASP.NET 相册上传代码
阅读量:4668 次
发布时间:2019-06-09

本文共 2224 字,大约阅读时间需要 7 分钟。

using System.Web.UI.WebControls.WebParts; using System.Data.SqlClient; using System.IO; public partial class 相册 : System.Web.UI.Page {     protected void Page_Load(object sender, EventArgs e)     { if(Page.IsPostBack)         { this.GridView1.DataSourceID = this.SqlDataSource1.ID; }     }     protected void LinkButton1_Click(object sender, EventArgs e)     {         MultiView1.SetActiveView(View2);     }     protected void LinkButton2_Click(object sender, EventArgs e)     {         MultiView1.SetActiveView(View1);     }     protected void Button1_Click(object sender, EventArgs e)     {         string filename = Path.GetFileName(File1.PostedFile.FileName);         string save = Server.MapPath("~/") + "image\\" + filename;         File1.PostedFile.SaveAs(save);         string filename1 = "~\\image\\" + filename;         string constr = @"server=2B13\SQLEXPRESS;database=zuoye;integrated security=SSPI";         SqlConnection con = new SqlConnection(constr);         con.Open();         string str1 = "insert into image values('" + Session["name"] + "','" + filename1 + "','" + this.TextBox1.Text + "')";         SqlCommand cmd = new SqlCommand(str1, con);         try         {             int b = cmd.ExecuteNonQuery();             this.Label4.Text = "恭喜您的图片上传成功!";         }         catch (Exception)         {             throw;         }     } } 用到了 什么啊 能不能解释下呢
我只给你解释最重要的部分 也是唯一重要的部分 记得给我最佳答案 string filename = Path.GetFileName(File1.PostedFile.FileName); //获得你锁上传文件的文件面
string save = Server.MapPath("~/") + "image\\" + filename; //在文件名的前面加上相对路径 比如~/image/文件名
File1.PostedFile.SaveAs(save);//保存在image文件夹下,save就是文件的相对路径
string filename1 = "~\\image\\" + filename; //filename等于相当路径~存储在数据库中,方便显示图片
string constr = @"server=2B13\SQLEXPRESS;database=zuoye;integrated security=SSPI"; //获得数据库连接字符串
SqlConnection con = new SqlConnection(constr); //连接数据库
con.Open(); //打开连接
string str1 = "insert into image values('" + Session["name"] + "','" + filename1 + "','" + this.TextBox1.Text + "')"; //定义SQL语句,这样写不好,要用存储过程哦 SqlCommand cmd = new SqlCommand(str1, con); //执行sql语句 try { int b = cmd.ExecuteNonQuery(); //返回受影响的行数 this.Label4.Text = "恭喜您的图片上传成功!"; }

转载于:https://www.cnblogs.com/zhhq/archive/2013/05/28/3103640.html

你可能感兴趣的文章
Ubuntu 14.04环境变量修改
查看>>
多线程Lock版生产者和消费者模式
查看>>
zoj3802:easy 2048 again(状压dp)
查看>>
Jenkins 自动化集成之路 Linux 安装 maven
查看>>
vue 自学笔记(七) 组件细节问题
查看>>
CSUOJ 1856 Sokoban 模拟
查看>>
List实体去重
查看>>
python函数回顾:abs()
查看>>
初识大数据(四. 大数据与人工智能的关系)
查看>>
netty 入门(一)
查看>>
Intellij Idea 15 下新建 Hibernate 项目以及如何添加配置
查看>>
《火星!火星!》
查看>>
大道至简读书笔记一
查看>>
php apache 配置后不能正常显示html文件的解决方法
查看>>
FILE类型指针的头文件
查看>>
牛客网暑期ACM多校训练营(第五场)J-plan (模拟)
查看>>
如何做一个跨平台的游戏App?
查看>>
五、椒盐排骨
查看>>
loj136 (最小瓶颈路,多次询问)
查看>>
4.1字符类型统计
查看>>