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

时间:2013-07-08 15:07:32

标签: c# database image drop-down-menu asp.net-4.0

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

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

这是我的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,依此类推直到最后一个图像。

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

How to put an image into selected postition and store in database

1 个答案:

答案 0 :(得分:0)

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

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);
}