MVC上传图像使用ado.net with Update保存在SQL中(我编辑任何文本框.图像消失)


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

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

这是我使用ado.net将图像保存到db的方式。希望这对您有帮助

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

最新更新