在数据库中上传多个图像

时间:2016-09-17 22:11:33

标签: asp.net

当我在数据库中插入多个图像时,我的图像名称未使用存储过程代码插入数据库中

upload.aspx.cs

 Dictionary<string, string> data = new Dictionary<string, string>();
            HttpFileCollection imgcoll = Request.Files;
for (int i = 0; i < imgcoll.Count; i++)
            {
                imgcoll[i].SaveAs(Server.MapPath("~/images/" + imgcoll[i].FileName));
                col.Add(new img() { img_name = imgcoll[i].FileName });

                data.Add("@Id", null);
                data.Add("@Name", FileUpload1.FileName);
                con.ins("img", "insert", data);
            }`DataList1.DataSource = col;
            DataList1.DataBind();

img.cs

public string img_name
{
    get;
    set;
}

连接类是

 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["crudConnectionString"].ConnectionString);

  public void ins(string pro, string insert_eve, Dictionary<string, string> table_col)
    {
        SqlCommand cmd = new SqlCommand(pro);
        cmd.Parameters.AddWithValue("@Event", insert_eve);
        //cmd.Parameters.AddWithValue("@Id", null);

        foreach (var pair in table_col)
        {
            cmd.Parameters.AddWithValue(pair.Key, pair.Value);
        }

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }

upload.aspx

<div>
    <asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />
    <br />
      <asp:Label ID="Label1" runat="server"></asp:Label>
    <br />
    <asp:Button ID="Button1" runat="server" Text="upload" OnClick="Button1_Click1" />
    <br />
    <br />

    <asp:DataList ID="DataList1" runat="server" RepeatColumns="3">
     <ItemTemplate>
        <div class="upload_image">
    <div class="imaage_div">

        <img src='images/<%#Eval("img_name")%>' width="100px" height="100px"/>
        <div class="delete">            
            <asp:ImageButton ID="ImageButton1" runat="server" style=" height: 20px; width: 20px" ImageUrl="~/images/Icon_delete.png" CommandArgument='<%# Eval("img_name") %>' OnCommand="ImageButton1_Command"/>
            </div>
    </div>                              
</div>
         </ItemTemplate>
    </asp:DataList>
    <br />

</div>
    <asp:Button ID="Button2" runat="server" Text="INsert" OnClick="Button2_Click" />

0 个答案:

没有答案