如何根据用户选择将图像放入数据库

本文关键字:图像 数据库 选择 何根 用户 | 更新日期: 2023-09-27 18:21:25

我有一个上传图像页面,其中有一个文件上传器控件和一个下拉列表。我的代码流程如下:

首先,用户使用文件加载程序从系统中选择一个文件,然后从下拉列表中选择部门并单击提交按钮。当用户单击提交按钮时,图像路径将保存在数据库中,图像将保存到选定的部门文件夹中。

这是我的aspx.cs文件

protected void Page_Load(object sender, EventArgs e)
{
    AutoNumber();
}
public void AutoNumber()
{
    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT COUNT(Priority) as Tot FROM Images", con);
    SqlDataReader dr;
    dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        int i = Convert.ToInt32(dr["tot"]);
        if (i > 0)
        {
            int j = i + 1;
            lblPriority.Text = "0" + j.ToString();
        }
        else
        {
            lblPriority.Text = "1";
        }
    }
    con.Close();
}
protected void btnSubmit_Click1(object sender, EventArgs e)
{
    //con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["WebGallery"].ConnectionString;
    string DepartmentID = ddlDepartment.SelectedValue;
    string Description = tbImageName.Text.Trim();
    string Priority = lblPriority.Text.Trim();
    //Get Filename from fileupload control
    string imgName = fileuploadimages.FileName.ToString();
    //sets the image path if exist then store image in that place else create one
    string imgPath = "Images/Departments/" + "" + ddlDepartment.SelectedValue + "/";
    bool IsExists = System.IO.Directory.Exists(Server.MapPath(imgPath));
    if (!IsExists)
        System.IO.Directory.CreateDirectory(Server.MapPath(imgPath));
    //then save it to the Folder
    fileuploadimages.SaveAs(Server.MapPath(imgPath + imgName));
    //Open the database connection
    con.Open();
    //Query to insert * into images into database
    SqlCommand cmd = new SqlCommand("insert into Images(ImageName, Description, Path, Priority,DepartmentID) values (@ImageName, @Description, @Path, @Priority,@DepartmentID)", con);
    //Passing parameters to query
    cmd.Parameters.AddWithValue("@ImageName", imgName);
    cmd.Parameters.AddWithValue("@Description", Description);
    cmd.Parameters.AddWithValue("@Path", imgPath + imgName);
    cmd.Parameters.AddWithValue("@Priority", lblPriority.Text);
    cmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
    cmd.ExecuteNonQuery();
    //Close dbconnection
    con.Close();
    tbImageName.Text = string.Empty;
}
}

现在我想要的是:

现在我想再添加一个下拉列表,根据数据库中存储的图像的优先级对哪些项目进行排序。当用户浏览一个图像,选择一个部门并为该图像选择优先级值时,数据库记录将以该优先级存储,并且已经处于该图像的图像将移动1,依此类推,直到最后一个图像。

这是我的相关参考问题。在那里,我没有得到任何解决方案,所以我想修改我的问题,再问一次。我希望你们不要介意伙计们:

如何将图像放入选定位置并存储在数据库中

如何根据用户选择将图像放入数据库

最后我得到了我想要的东西,我想这也会对其他人有所帮助——这是我的代码按钮,用于为所选优先级添加图像

protected void ChangePriority_Click(object sender, EventArgs e)
{
    string DepartmentID = ddlDepartment.SelectedValue;
    string Description = tbImageName.Text.Trim();
    string Priority = ddlPriority.SelectedValue;
    //Get Filename from fileupload control
    string imgName = fileuploadimages.FileName.ToString();
    //sets the image path if exist then store image in that place else create one
    string imgPath = "Images/Departments/" + "" + ddlDepartment.SelectedValue + "/";
    bool IsExists = System.IO.Directory.Exists(Server.MapPath(imgPath));
    if (!IsExists)
        System.IO.Directory.CreateDirectory(Server.MapPath(imgPath));
    //then save it to the Folder
    fileuploadimages.SaveAs(Server.MapPath(imgPath + imgName));
    //Open the database connection
    con.Open();
    //Query to insert * into images into database
    SqlCommand cmd = new SqlCommand("Update Images set Priority=Priority+1 where Priority>='" + ddlPriority.SelectedValue + "'" + "Insert into Images(ImageName,Description,Path,Priority,DepartmentID) values(@ImageName,@Description,@Path,@Priority,@DepartmentID)", con);
    //Passing parameters to query
    cmd.Parameters.AddWithValue("@ImageName", imgName);
    cmd.Parameters.AddWithValue("@Description", Description);
    cmd.Parameters.AddWithValue("@Path", imgPath + imgName);
    cmd.Parameters.AddWithValue("@Priority", Priority);
    cmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
    cmd.ExecuteNonQuery();
    //Close dbconnection
    con.Close();
    tbImageName.Text = string.Empty;
    Response.Redirect(Request.RawUrl);
}