-1

下面是我的用户界面和表格。我想在gridview中插入一个孩子的图像,它也将保存在数据库中。请协助我想如何实现这一目标。

表上图像的数据类型是varchar(MAX)

这是我到目前为止所做的。

(C#代码)

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data;
  using System.Data.SqlClient;
  using System.IO;
  using System.Configuration;

  namespace MyProject
  {
public partial class managechild : System.Web.UI.Page
{

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["lolConnectionString1"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {           
        BindGrid();
    }
} 
protected void BindGrid()
{
    string session = System.Web.HttpContext.Current.User.Identity.Name;
    Response.Cookies["uname"].Value = session;

    DataSet ds = new DataSet();
    conn.Open();
    string cmdstr = "SELECT * from child c join parent p on c.ParentId = p.ParentId join login l on l.Username = p.UserId where l.Username ='" + session + "'";
    SqlCommand cmd = new SqlCommand(cmdstr, conn);
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    adp.Fill(ds);
    gvUpload.DataSource = ds;
    gvUpload.DataBind();
    conn.Close();
}

protected void btnUpload_OnClick(object sender, EventArgs e)
{
        TextBox txtName = (TextBox)gvUpload.SelectedRow.FindControl("ChildName");
        FileUpload fuploadFile = (FileUpload)gvUpload.SelectedRow.FindControl("fUpload");
        Button btnUpload = (Button)gvUpload.SelectedRow.FindControl("btnUpload");

if (fuploadFile.HasFile)
{               
    string fileName = fuploadFile.FileName;
    string exten = Path.GetExtension(fileName); 
    //here we have to restrict file type            
    exten = exten.ToLower();
    string[] acceptedFileTypes = new string[4];            
    acceptedFileTypes[0] = ".jpg";
    acceptedFileTypes[1] = ".jpeg";
    acceptedFileTypes[2] = ".gif";
    acceptedFileTypes[3] = ".png";                               
    bool acceptFile = false;              
    for (int i = 0; i <= 3; i++)
    {
        if (exten == acceptedFileTypes[i])
        {                     
            acceptFile = true;
        }
    } 
    if (!acceptFile)
    {
        lblMsg.Text = "The file you are trying to upload is not a permitted file type!";
    }
    else
    {
        //upload the file onto the server                   
        fuploadFile.SaveAs(Server.MapPath("~/images/child/"+fileName));

        conn.Open();
        string cmdstr = "insert into Child (Image) values (@photo)";
        SqlCommand cmd = new SqlCommand(cmdstr, conn);
        cmd.Parameters.AddWithValue("@photo", "images/child/"+fileName);
        cmd.ExecuteNonQuery();
        conn.Close();
        BindGrid();
    }
}       
} 

}
}

在此处输入图像描述

用户界面代码 (asp.net)

    <asp:GridView ID="gvUpload" runat="server" AutoGenerateColumns="False" 
                ShowFooter="True" CellPadding="4" ForeColor="#333333" GridLines="None" 
                style="margin-left: 128px">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
   <Columns>
    <asp:TemplateField HeaderText="Child Name">
        <ItemTemplate>
            <asp:Label ID="lblName" runat="server" Text='<%#DataBinder.
                              Eval(Container.DataItem, "ChildName") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="Image">
    <ItemTemplate>
         <asp:Image ID="imgPhoto" runat="server" Width="100px" Height="120px"
                 ImageUrl='<%#DataBinder.Eval(Container.DataItem, "Image") %>' />
    </ItemTemplate>
    <ItemTemplate>
         <asp:FileUpload ID="fUpload" runat="server" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:Button ID="btnUpload" runat="server" Text="Upload"
                                           OnClick="btnUpload_OnClick" />
        </ItemTemplate>
    </asp:TemplateField>
    </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView> 
4

1 回答 1

2

在 SO 上发帖时,请确保在向其他人寻求完整解决方案之前至少展示一些自己解决问题的尝试。

一种方法是将图像作为二进制数据保存在数据库中,在这种情况下,您需要将 varbinary 列添加到表中。

另一种方法是只保存图像的路径并将图像保存在服务器上的某个文件夹中。

如果您没有很多图像,我建议您使用第二种解决方案,因为它更容易实现。只需查看如何上传图像文件,您就会走上正轨。

于 2013-11-10T15:16:47.470 回答