本文介绍了如何根据用户选择将图像放入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好编码器我有一个上传图片页面,其中我有文件上传器控件和一个下拉列表。我的代码流程 首先用户使用文件up-loader从系统中选择文件,然后从下拉列表中选择部门,并在用户点击时点击提交按钮在提交按钮图像路径保存在数据库和图像保存到选定的部门文件夹。 protected void Page_Load( object sender,EventArgs e) { AutoNumber(); } public void AutoNumber() { con.Open(); SqlCommand cmd = new SqlCommand( SELECT COUNT(优先级)为TOT FROM Images,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(); } 受保护 void btnSubmit_Click1( object sender,EventArgs e) { // con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings [WebGallery]。ConnectionString; string DepartmentID = ddlDepartment.SelectedValue; string 描述= tbImageName.Text.Trim(); string Priority = lblPriority.Text.Trim(); // 从文件上传控件获取文件名 string imgName = fileuploadimages.FileName.ToString(); // 设置图像路径(如果存在),然后将图像存储在该位置,否则创建一个 string imgPath = Images / Departments / + + ddlDepartment.SelectedValue + /; bool IsExists = System.IO.Directory.Exists(Server.MapPath(imgPath)); if (!IsExists) System.IO.Directory.CreateDirectory(Server.MapPath(imgPath)); // 然后将其保存到文件夹 fileuploadimages.SaveAs(服务器。 MapPath(imgPath + imgName)); // 打开数据库连接 con.Open(); // 将*插入图像到数据库的查询 SqlCommand cmd = new SqlCommand( 插入图像(ImageName,Description,Path) ,Priority,DepartmentID)值(@ ImageName,@ Description,@ Path,@ Priority,@ DepartmentID),con); // 将参数传递给查询 cmd.Parameters.AddWithValue( @ ImageName,imgName); cmd.Parameters.AddWithValue( @ Description,Description); cmd.Parameters.AddWithValue( @ Path,imgPath + imgName); cmd.Parameters.AddWithValue( @ Priority,lblPriority.Text); cmd.Parameters.AddWithValue( @ DepartmentID,DepartmentID); cmd.ExecuteNonQuery(); // 关闭dbconnection con.Close(); tbImageName.Text = string .Empty; } } 现在我想要的是: 现在我想再添加一个下拉列表,其中图像的优先级来自数据库。当用户浏览图像选择部门并选择优先级时,图像存储图像以该优先级和已经在该图像处的图像移动1,依此类推至最后一个图像。 这是我的相关参考问题他们我没有得到任何解决方案所以我想再修改我的问题再问我希望你不介意的人 http://stackoverflow.com/questions/17209459/how-to-put-an-image-into-selected-postition-and-store-in-database/17210510?noredirect=1#comment24930508_17210510 [ ^ ] 解决方案 最后我得到了答案: b $ b 受保护 void btnSubmit_Click1( object sender,EventArgs e) { // con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings [WebGallery] .ConnectionString; string DepartmentID = ddlDepartment.SelectedValue; string 描述= tbImageName.Text.Trim(); // string Priority = lblPriority.Text.Trim(); string Priority = ddlPriority.SelectedValue; // 从文件上传控件获取文件名 string imgName = fileuploadimages.FileName.ToString(); // 设置图像路径(如果存在),然后将图像存储在该位置,否则创建一个 string imgPath = Images / Departments / + + ddlDepartment.SelectedValue + /; bool IsExists = System.IO.Directory.Exists(Server.MapPath(imgPath)); if (!IsExists) System.IO.Directory.CreateDirectory(Server.MapPath(imgPath)); // 然后将其保存到文件夹 fileuploadimages.SaveAs(服务器。 MapPath(imgPath + imgName)); // 打开数据库连接 con.Open(); // 查询将*插入图像到数据库 SqlCommand cmd = new SqlCommand( 更新图像集优先级=优先级+ 1,其中优先级> =' + ddlPriority.SelectedValue + ' + 插入图像(ImageName,描述,路径,优先级,DepartmentID)值(@ ImageName,@ Description,@ Path, @ Priority,@ DepartmentID),con); // 将参数传递给查询 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(); // 关闭dbconnection con.Close(); tbImageName.Text = string .Empty; // Response.Redirect(Request.RawUrl); } Hi coders i have an upload image page in which i have file up-loader control and one drop-down list. flow of my codeFirst user select file from the system using file up-loader and then select department from the drop-down list and click on submit button when user click on submit button image path save in the database and images save into the selected department folder.protected void Page_Load(object sender, EventArgs e){ AutoNumber();}public void AutoNumber(){ con.Open(); SqlCommand cmd = new SqlCommand("SELECT COUNT(Priority) as Tot FROM Images", 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 DepartmentID = ddlDepartment.SelectedValue; string Description = tbImageName.Text.Trim(); string Priority = lblPriority.Text.Trim(); //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("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", lblPriority.Text); cmd.Parameters.AddWithValue("@DepartmentID", DepartmentID); cmd.ExecuteNonQuery(); //Close dbconnection con.Close(); tbImageName.Text = string.Empty;}}Now what i want :Now i want add one more drop-down list in which priority of images come from the database. when user browse image select department and select priority that image store image at that priority and image which is already at that image move by 1 and so on till the last image.this is my related reference question their i did'nt get any solution so i thought to modify my question ask again i hope you don't mind guyshttp://stackoverflow.com/questions/17209459/how-to-put-an-image-into-selected-postition-and-store-in-database/17210510?noredirect=1#comment24930508_17210510[^] 解决方案 Finally i got my answer:protected void btnSubmit_Click1(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); } 这篇关于如何根据用户选择将图像放入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 17:40