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

时间:2013-06-20 09:01:10

标签: c# asp.net sql-server image drop-down-menu

我所拥有的是一个页面,用于上传图像并将其路径保存到具有图像优先级的数据库中。 现在这只是一个例子,我在我的数据库中有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;     
}   
}

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

如何从所选优先级中放置图像 提前致谢

2 个答案:

答案 0 :(得分: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 + ";"); 

   }

}

答案 1 :(得分: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);
}