Asp.net图片上传access数据库以及图片显示;点击图片以后显示[图片,以及图片的

Access数据库:在数据库建立一个pic(id设为主键[自动编号],)的表,(pic_name[备注])用于存储上传图片的名字.[此博客全部程序都通过windowds 2003和vs2005测试]
Upload.aspx
<form id="form1" method="post" enctype="multipart/form-data" runat="server">     <div>         <asp:FileUpload ID="FileUpload1" runat="server" />&nbsp;<asp:Button ID="Button1"             runat="server" OnClick="Button1_Click" Text="Button" /><br />         <br />         <asp:Image ID="Image1" runat="server" /><br />         <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />         </div>     </form>
upload.cs
protected void Page_Load(object sender, EventArgs e)     {          }     protected void Button1_Click(object sender, EventArgs e)     {         string name = FileUpload1.FileName;//获取已上传的文件的名字         string size = FileUpload1.PostedFile.ContentLength.ToString();//获取已上传文件的大小         string type = FileUpload1.PostedFile.ContentType;//获取已上传文件mime内容类型         string type2 = name.Substring(name.LastIndexOf(".")+1);//得到文件的后缀名         string ipath = Server.MapPath("upimg") + "//" + name;//获取文件上传的实际路径         string fpath = Server.MapPath("upfile") + "//" + name;//获取文件上传的实际路径         string wpath = "upimg//" + name;//写到文件的虚拟路径
        //判断文件格式         if (type2 == "jpg" || type2 == "gif")         {             FileUpload1.SaveAs(ipath);//将文件保存到path路径里面             Image1.ImageUrl = wpath;//显示图片             Label1.Text = "文件名称:" + name + "<br>文件大小:" + size + "文字<br>文件类型是:" + type + "<br>后缀名" + type2 + "<br>实际路径" + ipath + "<br>虚拟路径:" + wpath;             //把图片名字存到数据库里面             //定义连接数据库对象             string conn = "provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath("../App_Data/data.mdb");             OleDbConnection oleConnection = new OleDbConnection(conn);             oleConnection.Open();             OleDbCommand myCommand = new OleDbCommand("insert into pic (pic_name) values ('"+ name+"')", oleConnection);             myCommand.ExecuteNonQuery();             oleConnection.Close();
        }         else         {             Image1.Visible = false;//将图片控件隐藏             FileUpload1.SaveAs(fpath);//将文件保存到别的文件夹             Label1.Text = "文件名称:" + name + "<br>文件大小:" + size + "文字<br>文件类型是:" + type + "<br>后缀名" + type2 + "<br>实际路径" + ipath + "<br>虚拟路径:" + wpath;         }     }
 
Image_show.aspx(用于显示上传以后图片的的页面)
<form id="form1" runat="server">     <div>         <asp:DataList ID="DataList1" runat="server" Font-Bold="False" Font-Italic="False"             Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Left"             RepeatDirection="Horizontal">             <ItemTemplate>             <a href="pro_show.aspx?id=<%# DataBinder.Eval(Container.DataItem,"id") %>">                 <img border="0" src="admin/upimg/<%# DataBinder.Eval(Container.DataItem,"pic_name") %>" />             </a>             </ItemTemplate>         </asp:DataList></div>     </form>
Image_show.aspx(cs)
protected void Page_Load(object sender, EventArgs e)     {         //创建连接数据库字符串         string conn = "provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath("App_Data/test.mdb");         OleDbConnection oleConnection = new OleDbConnection(conn);         try         {             //打开数据库连接             oleConnection.Open();             OleDbCommand cmd = new OleDbCommand("select * from pic", oleConnection);             OleDbDataAdapter dpt = new OleDbDataAdapter(cmd);             DataSet ds = new DataSet();             dpt.Fill(ds);             DataList1.DataSource = ds;             DataList1.DataBind();         }         finally         {             //关闭数据库             oleConnection.Close();         }     }
pro_show.aspx(点击图片以后,显示图片以及文字介绍)
<form id="form1" runat="server">     <div>         <asp:Label ID="Label1" runat="server" Text="Label" Width="142px"></asp:Label><asp:DataList ID="DataList1" runat="server">             <ItemTemplate>                  <img border="0" src="admin/upimg/<%# DataBinder.Eval(Container.DataItem,"pic_name") %>" />             </ItemTemplate>         </asp:DataList></div>     </form>
pro_show.cs
protected void Page_Load(object sender, EventArgs e)     {         string AuthorID = Request["id"].ToString();         //创建连接数据库字符串         string conn = "provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath("App_Data/test.mdb");         OleDbConnection oleConnection = new OleDbConnection(conn);
        OleDbCommand cmd = new OleDbCommand("select * from pic where [id]=" + AuthorID, oleConnection);         //打开数据库连接         oleConnection.Open();         try         {             OleDbDataAdapter dpt = new OleDbDataAdapter(cmd);             DataSet ds = new DataSet();             dpt.Fill(ds);             DataList1.DataSource = ds;             DataList1.DataBind();
            OleDbDataReader dr = cmd.ExecuteReader();             if (dr.Read())             {                 Label1.Text = dr["introduced"].ToString();             }
        }         catch (Exception error)         {             Response.Write(error.ToString());         }         finally         {             oleConnection.Close();         }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章