gpt4 book ai didi

c# - 关键字 'add' 附近的语法不正确

转载 作者:太空宇宙 更新时间:2023-11-03 22:57:55 26 4
gpt4 key购买 nike

我在学校有一个项目,我需要将我的注册页面与数据库连接起来。我有这段代码:

if (Request.Form["submit"] != null)
{
string fName = Request.Form["fName"];
string lName = Request.Form["lName"];
string Passwod = Request.Form["Passwod"];
string email = Request.Form["email"];
string add = Request.Form["add"];

string RegStatus;

if ((fName == "") || (lName == "") || (Passwod == "") || (email == "") || (add == ""))
{
RegStatus = ("missing data or wrong data");
}
else
{
string selectQuery = "SELECT * FROM " + "[Users]";
selectQuery += " WHERE ";
selectQuery += " email = '" + Request.Form["email"] + "'";

if (MyAdoHelper.IsExist(selectQuery))
{
RegStatus = ("email does not exists");
}
else
{
string insertQuery = "INSERT INTO [Users] (fName,lName,Passwod, email,add) VALUES ('";
insertQuery += fName + "', '" + lName +"','" + Passwod + "', '" + email + "','" + add +"')";
Response.Write(insertQuery);
MyAdoHelper.DoQuery(insertQuery);
RegStatus = ("Registeration was successful ");
}
}

Response.Write(RegStatus);
Response.End();
}

填充数据后(运行后)出现的错误是:

System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'add'.

来源错误:

public static void DoQuery(string sql)
{
SqlConnection conn = ConnectToDb();
conn.Open();
SqlCommand com = new SqlCommand(sql, conn);
com.ExecuteNonQuery(); //* it says the error is in this line. //*
com.Dispose();
conn.Close();
}

最佳答案

add 是 SQL 的关键字。如果您有一个这样命名的字段,则必须使用方括号:

INSERT INTO [Users] (fName,lName,Passwod, email,[add]) VALUES... 

此外,正如已经评论过的,使用参数而不是字符串连接非常重要:

关于c# - 关键字 'add' 附近的语法不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44520922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com