“ System.Data.SqlClient.SqlException:'')附近的语法不正确。'”错误

时间:2019-01-28 19:17:36

标签: c# sql asp.net model-view-controller

我是asp.net编程的新手

我不断收到此错误:

  

System.Data.SqlClient.SqlException:'')附近的语法不正确

附近

sqlcomm.ExecuteNonQuery();

这是我完整的代码:

[HttpPost]
public ActionResult Index(UserClass uc,HttpPostedFileBase file)
{
    string mainconn = ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString;

    SqlConnection sqlconn = new SqlConnection(mainconn);

    string sqlquery = "insert into [dbo].[MVCregUser] (Uname, Uemail, Upwd, Gender, Uimage) values (@Uname, @Uemail, @Upwd, @Gender, @Uimage,)";
    SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);

    sqlconn.Open();

    sqlcomm.Parameters.AddWithValue("@Uname",uc.Uname);
    sqlcomm.Parameters.AddWithValue("@Uemail", uc.Uemail);
    sqlcomm.Parameters.AddWithValue("@Upwd", uc.Upwd);
    sqlcomm.Parameters.AddWithValue("@Gender", uc.Gender);

    if(file != null && file.ContentLength > 0)
    {
        string filename = Path.GetFileName(file.FileName);
        string imgpath = Path.Combine(Server.MapPath("~//User-Images"), filename);
        file.SaveAs(imgpath);
    }

    sqlcomm.Parameters.AddWithValue("@Uimage", "~//User-Images"+file.FileName);

    sqlcomm.ExecuteNonQuery();
    sqlconn.Close();

    ViewData["Message"] = "User Record " + uc.Uname + " Is Saved Successfully";
    return View();
}

1 个答案:

答案 0 :(得分:3)

在这一行:

string sqlquery = "insert into [dbo].[MVCregUser] (Uname,Uemail,Upwd,Gender,Uimage) values (@Uname,@Uemail,@Upwd,@Gender,@Uimage,)";

最后的逗号是SQL语法。请删除它,然后再尝试这样:

string sqlquery = "insert into [dbo].[MVCregUser] (Uname,Uemail,Upwd,Gender,Uimage) values (@Uname,@Uemail,@Upwd,@Gender,@Uimage)";