2014-12-03 58 views
-3

我使用asp.net创建网站。到目前为止,我已完成一个注册页面,该页面将详细信息保存到数据库表中。检查数据库的用户名和密码

如何检查用户名和密码是否在该表中,然后让他们进入下一页?

这是我的代码注册;

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["userinfo.ConnectionString"].ConnectionString); 
conn.Open(); 

string insertQuery = "INSERT INTO [user] (UserName, FirstName, LastName, Email, Password, Country) VALUES (@uname, @fname, @lname, @email, @password, @country)"; 

SqlCommand comm = new SqlCommand(insertQuery, conn); 

comm.Parameters.AddWithValue("@uname", usernametextbox.Text); 
comm.Parameters.AddWithValue("@fname", fnametextbox.Text); 
comm.Parameters.AddWithValue("@lname", lnametextbox.Text); 
comm.Parameters.AddWithValue("@email", emailtextbox.Text); 
comm.Parameters.AddWithValue("@country", DropDownListcountry.Text); 
comm.Parameters.AddWithValue("@password", passwordtextbox.Text); 

comm.ExecuteNonQuery(); 

conn.Close(); 

我猜我需要创建一个SELECT查询,用if语句也许?

+0

显示你的努力,问题出在哪里?发布一些代码! – mybirthname 2014-12-03 15:58:35

+2

您将查询数据库...或[入门简介](http://msdn.microsoft.com/en-us/library/yh26yfzy%28v=vs.100%29.aspx) – 2014-12-03 15:58:39

+0

这是重复请检查链接 http://stackoverflow.com/questions/17871307/check-database-for-username-or-password-oleddb-connection – prasy 2014-12-03 15:58:46

回答

0

克里斯,

您对这个问题越来越下降,因为票你问的东西,你可以很容易地找出对自己使用谷歌。

这就是说,是的,你是正确的,你将需要使用SELECT语句。我不打算给你准确的.NET或SQL语法,因为这会剥夺你的学习体验。但是,弄清楚如何做一个SELECT COUNT查询。您基本上想要使用提供的用户名和密码来计算已存在的行数。而不是使用ExecuteNonQuery,你会使用ExecuteScalar,它返回一个值。

然后在你的.NET代码中,你会看到返回的计数值。如果它是0,继续。如果它大于0,则采取其他措施。

所有的说法,.NET有一些内置的工具,为你做这些工作。找到他们并使用它们!

未来,在来这里寻求帮助之前,尽量多花点时间做一些自己的研究。

祝你好运!

+0

:-)好吧,我看到有人已经给你准确的语法,以及内置.NET工具(ASP Membership)的名称。太糟糕了。自己学习对你来说更有帮助。 – 2014-12-03 16:21:49

0

如果您在登录页面中使用文本框输入用户名和密码。

 string connectionstring = WebConfigurationManager.ConnectionStrings["userinfo.ConnectionString"].ConnectionString; 
       string sqlstring; 
       sqlstring = "Select UserName,Password from user where UserName='" + 
Texboxusername.Text + "' and Password ='" + Textboxpassword.Text + "'"; 

       SqlConnection con = new SqlConnection(connectionstring); 
       SqlCommand command = new SqlCommand(sqlstring, con); 

       System.Data.SqlClient.SqlDataReader reader; 

       // open a connection with sqldatabase 
       con.Open(); 

       reader = command.ExecuteReader(); 

       if (reader.Read())//Reader.read() true if found in database 
       { 

     Response.Redirect("userHome.aspx"); 
     } 
     con.close(); 

第二种解决方案是使用表格Authentication.Add登录从工具箱中登录的设计page.Click它twice.İnside它相同的代码,而是Texboxusername.Text和Textboxpassword.Text使用Login.Username和登录。密码。

if (reader.Read()) 
{ e.Authenticated = true;} 
else{e.Authenticated=false;} 

最后在Web.config中添加这里面的某处<system.web> .. </system.web>;

<forms loginUrl="login.aspx" defaultUrl="userHome.aspx" timeout="60"/> 
    </authentication> 
    <authorization> 
     <deny users="?"/> 
    </authorization> 

经过身份验证的用户将自动重定向到defaultUrl,人们可以从loginUrl由于只访问。 如果你曾经有一个关于UnobtrusiveValidationMode的错误。 添加这里面<configuration>... </configuration>;

<add key="ValidationSettings:UnobtrusiveValidationMode" value="none"/> 
+0

好吧,我同意凯西。显然你不想强迫自己学习,但希望看到结果。但我喜欢直接给出解决方案,因为我还是有点懒惰:p – YourSolutionPartner 2014-12-06 23:13:40