本文介绍了如何把图像到选定postition和数据库存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么是上传图片并保存它们的路径到数据库中的图像的优先级的页面。
现在,这只是一个例子,我有我的数据库中有自己的优先级5的图像,例如

What i have is a page to upload images and save their path into database with priority of the images.Now this is just an example i have a 5 images in my database with their priority like

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

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

In my page i have File-Uploader control to upload file and drop-down list for priority in which priority comes from database.

现在我的问题是,当我浏览要上传的图片,并选择优先从下拉列表
图像应发生在该优先级和优先级为1的图像增加等类似

Now my question is when i browse image to upload and select priority from drop-down list image should place at that priority and priority for that image increase by 1 and so on like

如果我要上传图片6号,并将其存储在3个优先则数据库应该是这样的。

if i want to upload image number 6 and store it at 3 priority then database should be like this

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

这是我的code上传image.aspx.cs文件

This is my code for upload image.aspx.cs file

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

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

there is has no error it update as well as insert but it will not insert at the selected priority from the drop-down list it will add at the last and update the priority.

我如何可以把图像从选定的优先
在此先感谢

How can i put image from the selected priorityThanks in advance

推荐答案

最后我得到了我的答案

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

这篇关于如何把图像到选定postition和数据库存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 04:02