在数据库和filePath中上传图像

编程入门 行业动态 更新时间:2024-10-11 07:29:35
本文介绍了在数据库和filePath中上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我试图在数据库和文件路径中上传图像。我遇到的问题是每次运行并尝试插入详细信息图像我得到插入unsuccesfull的错误但图像保存在给定的文件路径中而不是数据库中。任何帮助都会很棒。 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [MyConnection ] .ToString()); SqlCommand cmd = new SqlCommand(); protected void Page_Load(object sender,EventArgs e) { } protected void Button1_Click(对象发送者,EventArgs e) { con.Open(); string strfilepath = Path.GetFileName(FileUpload1.PostedFile.FileName.ToString()); string mypath = Server.MapPath( Images /+ strfilepath); FileUpload1.PostedFile.SaveAs(mypath); { string ext = Path.GetExtension(FileUpload1 .FileName); if(ext ==.jpg|| ext ==。png) { SqlCommand cmd = new SqlCommand(插入模型(Model_Company,Image_Name,Model_No,M odel_Price,Image)values(@ Model_Company,@ Image_Name,@ Model_No,@ Model_Price,@ Image),con); cmd.Parameters.Add(@ Model_Company,SqlDbType.VarChar,50 ).Value = TextBox1.Text; cmd.Parameters.Add(@ Image_Name,SqlDbType.VarChar,50).Value = TextBox2.Text; cmd。 Parameters.Add(@ Model_No,SqlDbType.VarChar,50).Value = TextBox3.Text; cmd.Parameters.Add(@ Model_Price,SqlDbType.VarChar,50).Value = TextBox4.Text; cmd.Parameters.AddWithValue(@ Image,Images /+ strfilepath); imgdisplay.ImageUrl =(Images /+ strfilepath ); cmd.ExecuteNonQuery(); con.Close(); lblMsg.ForeColor = Color.Green; lblMsg.Text =successl; } else { lblMsg.ForeColor = Color.Red; lblMsg.Text =Insert Unsuccessfull; } } }

Hello guys, I trying to upload a image in database and also in filepath.The problem im getting is everytime i run and try to insert the details with Image i get the error of insert unsuccesfull but the image was saved in given filepath not in database. Any help will be greatfull. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ToString()); SqlCommand cmd = new SqlCommand(); protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { con.Open(); string strfilepath = Path.GetFileName(FileUpload1.PostedFile.FileName.ToString()); string mypath = Server.MapPath("Images/" + strfilepath); FileUpload1.PostedFile.SaveAs(mypath); { string ext = Path.GetExtension(FileUpload1.FileName); if (ext == ".jpg" || ext == ".png") { SqlCommand cmd = new SqlCommand("insert into Model(Model_Company,Image_Name,Model_No,Model_Price,Image)values(@Model_Company,@Image_Name,@Model_No,@Model_Price,@Image)", con); cmd.Parameters.Add("@Model_Company", SqlDbType.VarChar, 50).Value = TextBox1.Text; cmd.Parameters.Add("@Image_Name", SqlDbType.VarChar, 50).Value = TextBox2.Text; cmd.Parameters.Add("@Model_No", SqlDbType.VarChar, 50).Value = TextBox3.Text; cmd.Parameters.Add("@Model_Price", SqlDbType.VarChar, 50).Value = TextBox4.Text; cmd.Parameters.AddWithValue("@Image", "Images/" + strfilepath); imgdisplay.ImageUrl = ("Images/" + strfilepath); cmd.ExecuteNonQuery(); con.Close(); lblMsg.ForeColor = Color.Green; lblMsg.Text = "successfull"; } else { lblMsg.ForeColor = Color.Red; lblMsg.Text = "Insert Unsuccessfull"; } } }

推荐答案

如果您收到错误,那么您需要先查看异常详细信息,然后尝试查找到底发生了什么。一种方法是将您的代码包含在 try ... catch 块中并查看实际异常: If you are getting an error, then you need to start by looking at the exception detail to try and find out exactly what is happening. One way is to enclose your code in a try...catch block and look at the actual exception: protected void Button1_Click(object sender, EventArgs e) { try { con.Open(); ... } catch (Exception ex) { Console.Writeline(ex.ToString()); } }

在方法的第一行放置一个断点并在调试器中插入也是一个好主意 - 它可以让你看看它到底发生了什么当它发生的时候。 但是直到你知道所报告的错误的细节是什么,没有人能做到。

Putting a breakpoint on the first line of the method and stepping through in the debugger as well is a good idea - it lets you look at exactly what it going on while it is happening. But until you know what the detail for the reported error is, there isn't much anyone can do.

更多推荐

在数据库和filePath中上传图像

本文发布于:2023-06-02 01:27:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/430946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   上传   数据库   filePath

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!