本文介绍了将图像插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用休整代码将图像插入数据库.

I want to insert the image into the database using the fallowing code.

protected void btnUpload_Click(object sender, EventArgs e)
   {
       string file = "";
       con.Open();
       file = "~/images/" + FileUpload1.FileName;
       FileUpload1.SaveAs(Server.MapPath(file));
     SqlCommand cmd = new SqlCommand("insert into image2 values('" + file + "'", con);
       int i = Convert.ToInt32(cmd.ExecuteNonQuery());
       con.Close();
       if (i > 0)
       {
           Label1.Text = "image uploaded succesfully";
       }

   }


当我执行此操作时,出现了错误提示.


When I am executing this one am getting the fallowing error.

Server Error in '/WebSite15' Application.

Incorrect syntax near '~/images/img1.png'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '~/images/img1.png'.

Source Error:


Line 27:         FileUpload1.SaveAs(Server.MapPath(file));
Line 28:       SqlCommand cmd = new SqlCommand("insert into image2 values('" + file + "'", con);
Line 29:         int i = Convert.ToInt32(cmd.ExecuteNonQuery());
Line 30:         con.Close();
Line 31:         if (i > 0)

Source File: e:\sree\WebSite15\photos.aspx.cs    Line: 29

Stack Trace:


[SqlException (0x80131904): Incorrect syntax near '~/images/img1.png'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1951450
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4849003
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2394
   System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) +192
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +317
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
   photos.btnUpload_Click(Object sender, EventArgs e) in e:\sree\WebSite15\photos.aspx.cs:29
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565


请帮忙.


Please help from this.

推荐答案

SqlCommand cmd = new SqlCommand("insert into image2 values(''" + file + "'')", con);



这篇关于将图像插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:22
查看更多