我一直在youtube上关注注册工作的注册和登录页面的教程,但它允许我一遍又一遍地注册相同的细节。
代码如下
if(IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["regConnectionString"].ConnectionString);
conn.Open();
string checkuser = "select count(*) from users where username='"+ Usernametxt.Text +"'";
SqlCommand com = new SqlCommand(checkuser, conn);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp == 1)
{
Response.Write("User already exists");
}
conn.Close();
}
}
因此它绕过了这段代码,当它说用户已经存在时,任何人都可以看到代码问题或提供解决方案
答案 0 :(得分:0)
使用!IsPostBack代码不仅用于IsPostBack
if(!IsPostBack)
{
}
答案 1 :(得分:0)
我不确定但是在测试时已经有可能已经添加了多次用户名。所以如果你使用它会更好
if (temp > 0)
{
Response.Write("User already exists");
}
conn.Close();
无论如何,如果这是注册用户,我更喜欢使用一个过程来检查数据库中的用户名,如果用户名不存在则给出1,如果用户名已经存在则给出-1然后你可以相应地在代码隐藏中编写代码。
答案 2 :(得分:0)
试试这个
if(IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["regConnectionString"].ConnectionString);
conn.Open();
string checkuser = "SELECT TOP 1 username FROM users WHERE username='"+ Usernametxt.Text +"'";
SqlCommand com = new SqlCommand(checkuser, conn);
string temp = com.ExecuteScalar().ToString();
if (!string.IsNullOrEmpty(temp))
{
Response.Write("User already exists");
}
conn.Close();
}
使用此功能,您只需在表格中搜索,直到找到一条记录(TOP 1
),而不是整个表格(COUNT(*)
)