看不懂 ASP.NET 相册上传代码

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 = "恭喜您的图片上传成功!"; }

你可能感兴趣的:(asp.net)