0

我所拥有的是一个页面,用于上传图像并将其路径保存到数据库中,并具有图像的优先级。现在这只是一个例子,我的数据库中有 5 张图像,它们的优先级如下

ID  ImageName  Description  Path  Priority
 1    1.jpg      Hello      xxxx   1
 2    2.jpg       hi        xxxx   2
 3    3.jpg       how       xxxx   3
 4    4.jpg       good      xxxx   4
 5    5.jpg       bye       xxxx   5

在我的页面中,我有 File-Uploader 控件来上传文件和下拉列表的优先级,其中优先级来自数据库。

现在我的问题是,当我浏览要上传的图像并从下拉列表中选择优先级时,图像应该放在该优先级,并且该图像的优先级增加 1,依此类推

如果我想上传 6 号图像并将其存储在 3 优先级,那么数据库应该是这样的

ID  ImageName  Description  Path  Priority
 1    1.jpg      Hello      xxxx   1
 2    2.jpg       hi        xxxx   2
 6    6.jpg       you       xxxx   3
 3    3.jpg       how       xxxx   4
 4    4.jpg       good      xxxx   5
 5    5.jpg       bye       xxxx   6

这是我上传 image.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 TestImages", 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 Priority = lblPriority.Text.Trim();
    //Get Filename from fileupload control
    string imgName = fileuploadimages.FileName.ToString();
    //sets the image path
    string imgPath = "Images/"+""+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 images name and Description into database
    SqlCommand cmd = new SqlCommand("Insert into TestImages(ImageName,Description,Path,Priority) values(@ImageName,@Description,@Path,@Priority)" + "Update TestImages set Priority=Priority+1 where Priority='" + ddlPriority.SelectedValue + "'", con);
    //Passing parameters to query
    cmd.Parameters.AddWithValue("@ImageName", imgName);
    cmd.Parameters.AddWithValue("@Description", tbImageName.Text);
    cmd.Parameters.AddWithValue("@Path", imgPath + imgName);
    cmd.Parameters.AddWithValue("@Priority", lblPriority.Text);
    cmd.ExecuteNonQuery();
    //Close dbconnection
    con.Close();
    tbImageName.Text = string.Empty;     
}   
}

它更新和插入都没有错误,但它不会以从下拉列表中选择的优先级插入,它将在最后添加并更新优先级。

我如何从选定的优先级中放置图像提前谢谢

4

2 回答 2

0

终于我得到了答案

 protected void ChangePriority_Click(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();
    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);
}
于 2013-07-11T12:14:39.573 回答
0

您必须更新数据库中的每一行。尝试这样的事情:

Image newImage;        //Your image uploaded
List<Image> listImages; //All the images from your DB (before insertion)

foreach(Image img in listImages) {
   if(img['Priority'] < newImg['Priority']) 
   {
      SqlCommand cmd = new SqlCommand(
                  "UPDATE TestImage SET Priority=" + img['Priority'] - 1 + ";"); 
   }
   else
   {
      SqlCommand cmd = new SqlCommand(
                  "UPDATE TestImage SET Priority=" + img['Priority'] + 1 + ";"); 

   }

}
于 2013-06-20T09:48:22.590 回答