从Gridview检索数据库中的图像和更新图像?

时间:2013-10-24 09:57:50

标签: asp.net sql asp.net-mvc database c#-2.0

我想从网格视图中检索数据库中的图像和更新图像? 请告知

Zainul

2 个答案:

答案 0 :(得分:0)

尝试以这种方式进行编辑和检索:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ci_id" DataSourceID="SqlDataSource1">
            <Columns>
               <asp:TemplateField>
                   <EditItemTemplate>
                       <asp:FileUpload ID="FileUpload1" runat="server" /><asp:Button ID="Button1" runat="server" Text="Button" />
                   </EditItemTemplate>
                   <ItemTemplate>
                       <asp:Image ID="Image1" runat="server" />
                   </ItemTemplate>
               </asp:TemplateField>               
            </Columns>            
 </asp:GridView>  

答案 1 :(得分:0)

 protected void btnSubmit_Click(object sender, EventArgs e)
{
//Get Filename from fileupload control
string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
//Save images into Images folder
fileuploadimages.SaveAs(Server.MapPath("Images/"+filename));
//Getting dbconnection from web.config connectionstring
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
//Open the database connection
con.Open();
//Query to insert images path and name into database
SqlCommand cmd = new SqlCommand("Insert into ImagesPath(ImageName,ImagePath) values(@ImageName,@ImagePath)", con);
//Passing parameters to query
cmd.Parameters.AddWithValue("@ImageName", filename);
cmd.Parameters.AddWithValue("@ImagePath", "Images/" + filename);
cmd.ExecuteNonQuery();
//Close dbconnection
con.Close();
Response.Redirect("~/Default.aspx");
}

参考链接:

http://www.aspdotnet-suresh.com/2011/03/how-to-save-images-into-folder-and.html