我正在尝试创建一个管理仪表板,并在同一仪表板中选择添加文章。由于某种原因,我收到此错误:
System.Data.SqlClient.SqlException:关键字'file'附近的语法不正确。
这是我尝试构建的“新闻门户”所用代码的一部分。这是仪表板aspx页面后面的服务器端代码。
protected void Publish_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con.Open();
string command = "INSERT INTO Articles(title,author,content,file,tags) VALUES(@title,@author,@content,@file,@tags)";
SqlCommand cmd = new SqlCommand(command, con);
cmd.Parameters.AddWithValue("@title", Title1.Text);
cmd.Parameters.AddWithValue("@author", Author.Text);
cmd.Parameters.AddWithValue("@content", Content.Text);
string strImg = System.IO.Path.GetFileName(File1.PostedFile.FileName);
cmd.Parameters.AddWithValue("@file", strImg);
cmd.Parameters.AddWithValue("@tags", Tag.Text);
cmd.ExecuteNonQuery();
con.Close();
File1.PostedFile.SaveAs(Server.MapPath("Images\\") + strImg);
}
每按一次“发布”按钮,我都会收到此错误。
答案 0 :(得分:2)
可能是因为file
是reserved word,所以您需要用方括号将其包围:
string command = "INSERT INTO Articles(title,author,content,[file],tags) VALUES(@title,@author,@content,@file,@tags)";