MVC UPLOAD IMAGE使用带有更新的ado.net保存在SQL中(我编辑任何文本框。图像消失)

时间:2017-08-31 11:32:52

标签: asp.net asp.net-mvc

[HttpPost]
public ActionResult Create(Showroom showroom)
{
    objShowroom.InsertShowroomDetails(showroom);
    return RedirectToAction("Index");
}
  1. 我有单独的数据访问层
  2. 如何更新数据库中的上传图片
  3. 插入查询没问题
  4. 如果我编辑任何文本框图像将在回发或往返中消失

    5. 5.我将使用非实体框架,因此我想使用ado.net连接模型

    模型文件夹:

           public int UpdateShowroomDetails(Showroom objShowroom)
     {
        sqlCommandText = "Update Showroom Set CARIMAGE='" + objShowroom.CARIMAGE + "', CARNAME='" + objShowroom.CARNAME + "', CARBRAND='" + objShowroom.CARBRAND + "', PRICE=" + objShowroom.PRICE + " Where ID="+objShowroom.ID ;
        return objDAL.ExecuteNonQuery(sqlCommandText);
    }
    

1 个答案:

答案 0 :(得分:0)

以下是我使用ado.net将图像保存到数据库的方法。希望这会对你有所帮助

string fileName = string.Empty;
                    string servr_path = string.Empty;
                    string serviceId = string.Empty;
                    string planId = string.Empty;


                    if (IdProofFile.HasFile && IdProofFile.PostedFile.FileName != "")
                    {
                        string fileType = IdProofFile.PostedFile.ContentType;
                        if (fileType == "image/jpg" || fileType == "image/jpeg" || fileType == "image/png" || fileType == "image/gif")
                        {
                            int fileSize = IdProofFile.PostedFile.ContentLength;
                            if (fileSize <= 2097152)
                            {
                                //fileName = System.IO.Path.GetFileName(IdProofFile.PostedFile.FileName);
                                fileName = "ID-" + txt_FirstName.Text + "_" + txt_LstName.Text + DateTime.Now.ToString("ddmmyy_hhMMss") + Path.GetExtension(IdProofFile.PostedFile.FileName);
                                string serverFolder = Server.MapPath("idProof//");
                                if (!System.IO.Directory.Exists(serverFolder))
                                {
                                    System.IO.Directory.CreateDirectory(serverFolder);
                                }
                                servr_path = serverFolder + fileName;

                                foreach (int i in ddlservices.GetSelectedIndices())
                                {
                                    serviceId += "," + ddlservices.Items[i].Value;
                                }

                                 IdProofFile.SaveAs(servr_path);
                                }

                                    }
                                    else
                                    {

                                    }
                                }
                                else
                                {

                                }

                            }
                            else
                            {
                                //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "mask();", true);

                                mpepopup.Show();
                                lblMsg.Text = "Min File Size Must Be Greater Than 5 KB and Less Than 2 MB";
                            }
                        }
                        else
                        {
                            mpepopup.Show();
                            // ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "mask();", true);
                            lblMsg.Visible = true;
                            lblMsg.Text = "Please upload an image of *.jpg/*.jpeg/*.png/*.gif/*.bmp file type only!!!";
                        }
                    }
                    else
                    {
                        mpepopup.Show();
                        lblMsg.Text = "ID Proof File Not Selected.";
                    }