【问题标题】:How to put an image into selected postition and store in database如何将图像放入选定位置并存储在数据库中
【发布时间】:2013-06-20 09:01:10
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我必须在我的页面加载或按钮单击或下拉列表的 selectindex chage 上放置该代码
    • 我知道你想说什么,但是该代码应该放在哪里,这样它才能为我工作,你能把代码放在我的代码中告诉我吗
    • 您可以在插入新图片之前使用它。
    • 因为当我在插入之前放置该代码时,它会给我很多错误
    • Cannot apply indexing with [] to an expression of type 'System.Web.UI.WebControls.Image' 这是我遇到的错误
    【解决方案2】:

    我终于得到了答案

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

    【讨论】:

      猜你喜欢
      • 2011-05-25
      • 2019-05-07
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 2012-03-10
      • 2012-06-08
      相关资源
      最近更新 更多