【发布时间】:2017-06-13 20:11:36
【问题描述】:
我在学校有一个项目,我需要将我的注册页面与数据库连接起来。 我有这个代码:
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:关键字附近的语法不正确 '添加'。
来源错误:
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();
}
【问题讨论】:
-
如果您正在学习 SQL,请学习使用参数化查询。不要修改查询字符串。这只会导致语法错误和 SQL 注入漏洞。