大家好, 我试图在数据库和文件路径中上传图像。我遇到的问题是每次运行并尝试插入详细信息图像我得到插入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中上传图像
发布评论