2013-06-20 53 views
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 

在我的網頁我有文件的上傳控件上載文件和下拉列表的優先級,其中優先來自數據庫。

現在我的問題是,當我瀏覽要上傳的圖片,並選擇優先從下拉列表中 形象應該是優先級和優先級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;  
} 
} 

它沒有錯誤更新以及插入,但它不會從最後添加的下拉列表中以選定的優先級插入並更新優先級。

我怎麼可以把圖像從選定的優先 預先感謝

回答

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

    } 

} 
+0

在那裏我必須把該代碼在我的網頁加載或點擊按鈕或在下拉列表中的selectindex恰克 – amitesh

+0

我得到你想要的想要說,但在哪裏放置該代碼,以便它可以爲我工作,您可以通過將代碼放入我的代碼中顯示我 – amitesh

+0

您可以在插入新圖像之前使用它。 – Alex

相關問題