2014-12-03 106 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