我試圖用SQL INSERT INTO將剛上傳的img源插入到數據庫中。INSERT INTO - 在SQL語句末尾缺少分號(;)
這是我得到的行:
「缺少分號(;)在SQL語句的結束。」
錯誤。
command.CommandText = "INSERT INTO users (pic) VALUES (Images/"+fileName+") WHERE id="+theId;
這是整個.aspx.cs文件:
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections.Generic;
public partial class CS : System.Web.UI.Page
{
protected void Upload(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
string theId = Request.Cookies["Logged"].Value;
System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection();
connection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Etay\Documents\Visual Studio 2012\WebSites\Josef\Shared\users.mdb";
try
{
System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand();
command.Connection = connection;
connection.Open();
command.CommandText = "INSERT INTO users (pic) VALUES (Images/"+fileName+") WHERE id="+theId;
int rows = command.ExecuteNonQuery();
Response.Redirect("~/signIn.cshtml");
}
finally
{
connection.Close();
}
}
}
}
請不要將文件名和id連接到SQL語句中。使用參數,或者你自己設置SQL注入。 – BlakeH
刪除sql-server標記,使用MS Access。你得到不正確的MS Access代碼答案(OleDb) – Crowcoder
重定向的正確方法是:'Response.Redirect(url,false); \t Context.ApplicationInstance.CompleteRequest();' – Crowcoder