asp.net System.Data.SqlClient.SqlException:'F'附近的语法不正确,字符串')'后有未闭合的引号,'

olhwl3o2  于 2023-03-09  发布在  .NET
关注(0)|答案(1)|浏览(110)

我在Visual Studio 2019中使用ASP.NET和C#创建了一个Web表单。当我运行代码时,我收到一个错误:
System.Data.SqlClient.SqlException:“F”附近的语法不正确。字符串“)”后有未闭合的引号
我也不知道为什么。
我试图查找未闭引号,但找不到

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace site.pages
{
    public partial class register : System.Web.UI.Page
    {
        public string st = "";
        public string msg = "";
        public string sqlmsg = "";
        public string yrborn = "";
        public string tableByform;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Form["submit"] != null)
            {
                string fileName = "userDB.mdf";
                string tableName = "usersTbl";

                string uname = Request.Form["uname"];
                string Fname = Request.Form["Fname"];
                string lname = Request.Form["lname"];
                string email = Request.Form["email"];
                string yearBorn = Request.Form["YearBorn"];
                string gender = Request.Form["gender"];
                string City = Request.Form["City"];
                string Password = Request.Form["password"];
                string verify_password = Request.Form["vpassword"];
                string hobies = Request.Form["hobies"].ToString();
                char hob1 = 'F';
                char hob2 = 'F';
                char hob3 = 'F';
                char hob4 = 'F';
                if (hobies.Contains('1')) hob1 = 'T';
                if (hobies.Contains('2')) hob2 = 'T';
                if (hobies.Contains('3')) hob3 = 'T';
                if (hobies.Contains('4')) hob4 = 'T';
                int yborn = int.Parse(yearBorn);
                int prefix = int.Parse(Request.Form["prefix"]);
                int phone = int.Parse(Request.Form["phone"]);
                tableByform += "<table>";
                tableByform += "<tr><td> username:</td><td>" + uname + "</td></tr>";
                tableByform += "<tr><td> first name:</td><td>" + Fname + "</td></tr>";
                tableByform += "<tr><td> last name:</td><td>" + lname + "</td></tr>";
                tableByform += "<tr><td> email:</td><td>" + email + "</td></tr>";
                tableByform += "<tr><td> gender:</td><td>" + gender + "</td></tr>";
                tableByform += "<tr><td> YearBorn:</td><td>" + yearBorn + "</td></tr>";
                tableByform += "<tr><td> City:</td><td>" + City + "</td></tr>";
                tableByform += "<tr><td> prefix:</td><td>" + prefix + "</td></tr>";
                tableByform += "<tr><td> phone:</td><td>" + phone + "</td></tr>";
                tableByform += "<tr><td> hobies</td><td>" + hobies + "</td><td>";
                if (hob1== 'T')
                {
                    tableByform += "singing,";
                }

                if (hob2 == 'T')
                {
                    tableByform += "playing,";
                }

                if (hob3 == 'T')
                {
                    tableByform += "swimming,";
                }

                if (hob4 == 'T')
                {
                    tableByform += "eating,";
                }

                tableByform += "</td></tr>";
                tableByform += "<tr><td> Password:</td><td>" + Password + "</td></tr>";
                tableByform += "<tr><td> verify password:</td><td>" + verify_password + "</td></tr>";
                tableByform += "</table>";

                string sqlSelect = $"SELECT * FROM {tableName} WHERE uname = '{uname}'";

                if (Helper.IsExist(fileName, sqlSelect))
                {
                    st = "this user is taken";
                    sqlmsg = sqlSelect;
                }
                else
                {
                    string sqlInsert = $"insert into {tableName}";
                    sqlInsert += $"values('{uname}', N'{Fname}', N'{lname}', ";
                    sqlInsert += $"'{email}', {yearBorn}, '{gender}', '{prefix}', '{phone}', ";
                    sqlInsert += $"N'{City}', '{hob1}, '{hob2}', '{hob3}','{hob4}','{Password}')";
                    sqlmsg = sqlInsert;
                    Helper.DoQuery(fileName, sqlInsert);
                    msg = "Succsses";
                }
            }
        }
    }
}
3npbholx

3npbholx1#

我注意到您的SQL语句中有一个问题。
sqlInsert += $"N'{City}', '{hob1}, '{hob2}', '{hob3}', '{hob4}', '{Password}')";中,应在'{hob1}之后添加'
就像这样:

sqlInsert += $"N'{City}', '{hob1}', '{hob2}', '{hob3}', '{hob4}', '{Password}')";

同时,你应该参考评论中的建议,这可以使你的项目运行得更稳定。

相关问题